Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I have a menu that is populated dynamically:

<div ng-controller="menuDinamicoController as vm">
  <div ng-show="isAutenticado">
     <img src="{{vm.fotoUser}}" id="imagemUsuario" width="50px" />
     <label id="nomeUsuario" ng-model="nomeUser">{{vm.nomeUser}}</label>
    <div id="menu">
       <ul>
         <li ng-repeat="x in menu">
            <a href="{{x.Link}}" ng-click="vm.{{x.Id}}()">{{x.Nome}}</a>
         </li>
       </ul>
    </div>
 </div>

The structure of the link is being returned correctly:

<a href="#" ng-click="vm.sair()" class="ng-binding">Sair</a>

The controller has the following function:

vm.sair = function () {
    $cookieStore.remove("Usuario");
    $cookieStore.remove("Token");
    $location.path("/");
};

However, the ng-click isn't calling the function. And get the following error: Syntax Error: Token 'x.Id' is at column {2} of the expression [{3}] starting at [{4}].

share|improve this question

3 Answers 3

up vote 2 down vote accepted

You should not really need interpolation {{ }} inside the ng-click attribute.

If x.Id is a dynamically created function you should use the [] syntax instead:

ng-click="vm[x.Id]()"

To help further, please provide an example (with snippet, jsbin, or plunker).

share|improve this answer

You need to use [] object notation when passing variables as object keys in javascript. Also since you are passing as veriable, don't use expression around the variable

Try:

<a href="{{x.Link}}" ng-click="vm[x.Id]()">{{x.Nome}}</a>
share|improve this answer

The requirement itself is so Weird..!!!!! YOu will never have such type of functions for ng-clicks''!!! Even if you, it is that you will have to create 100's of functions for 100 objects in the data???

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.