Problem ng-click with AngularJS

3

I have a button:

<a ng-click="like">Gostar</a>

When someone clicks on it, it performs the action and it is replaced by jQuery with the other button:

<a ng-click="unlike">Desgostar</a>

But when you click the replaced buttons, they do not take any action. The ng-click function does not work, only when they are overwritten, when the reloads the normal work page. Does anyone know how to solve this?

    
asked by anonymous 16.01.2016 / 01:40

1 answer

1

This is because the new content did not pass through the Angular compiler.

This is an example of precompiling before inserting content via jQuery:

$("#conteudo").html(
  $compile(
    "<button ng-click='count = count + 1' ng-init='count=0'>Increment</button><span>count: {{count}} </span>"
  )(scope)
);

Tip - Instead of manipulating DOM objects, control visibility from within the Angular. Example:

<a ng-click="like" ng-if="!item.liked">Gostar</a>
<a ng-click="unlike" ng-if="item.liked">Desgostar</a>

So, your application displays one button or the other depending on the liked property of the item object.

    
16.01.2016 / 02:15