javascriptangularjsangularjs-ng-clickng-showangularjs-nvd3-directives

Angularjs parse error sometimes when using brackets


In the following code sample why are the brackets necessary around position[0].position in the ng-click directive in the anchor element but not in the ng-show directive in the divs?

<div ng-controller="PlayersController as pl">
  <section ng-init="tab = 'goalkeepers'">
    <li ng-repeat="position in pl.players">
      <a href ng-click="tab = {{position[0].position}}">{{position[0].position}}</a>
    </li>
  </section>
  <div ng-repeat="position in pl.players">
    <div ng-repeat='player in position' ng-show="tab === position[0].position">
      <h2 ng-show='$first'>{{player.position}}</h2>
      <h3>{{player.name}}</h3>
      <h4>{{player.price | currency: '£': 0}} {{player.score}}</h4>
    </div>
  </div>
</div>  

Does it have to do with setting equality versus checking for equality? Is it related to the nested ng-repeat?

When I add brackets around the equality check in ng-show in the div element I get a parse error, why?


Solution

  • In Angular expressions need to be within the curly-brace bindings, where as Angular directives do not.

    As we understand that ng-click is a directive you don't need to add curly-brace there.