0

I have the following snippet and it gave me parsing error.

<a class="pointer" role="menuitem" ng-repeat="rec in cfg.popupAssoc" ng-click="followSession(\"{{$index}}\")" tabindex="{{$index + 2}}"
    {{rec.popupEntry}}
</a>

after I changed \"{{$index}}\" to this, it doesn't have the compile error.

Any idea why? Thanks.

UPDATE1

The basic problem is, I want to pass something (could be a string with white space) to a function, but can't figure a way to do it. The following code is a naive attempt but it gives angular parsing error.

<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl"> 

<table>
  <tr >
    <td ng-click="dum();">ONE</td>
    <td>TWO</td>
  </tr>
  <tr ng-repeat="x in names">
    <td ng-click="dum({{x.Name}});" myid="{{$index + 1}}">{{ x.Name }}</td>
    <td>{{ x.Country }}</td>
  </tr>
</table>

</div>

<script>
obj1 = "haha";
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("http://www.w3schools.com/angular/customers.php")
    .success(function (response) {$scope.names = response.records;});
    $scope.dum = function(x) { obj1 = x; console.log(x); }
});

</script>

</body>
</html>
3
  • can you create a fiddle? Commented Jul 17, 2015 at 18:52
  • Not necessarily the cause of the error, but you can just use single quotes instead of escaping your double quotes Commented Jul 17, 2015 at 18:59
  • Why does the "full" code not contain the snippet? What do those two pieces of code have to do with each other? Commented Jul 17, 2015 at 22:20

2 Answers 2

2

You need to update from

<a class="pointer" role="menuitem" ng-repeat="rec in cfg.popupAssoc" ng-click="followSession(\"{{$index}}\")" tabindex="{{$index + 2}}"
    {{rec.popupEntry}}
</a>

to

<a class="pointer" role="menuitem" ng-repeat="rec in cfg.popupAssoc" ng-click="followSession($index)" tabindex="{{$index + 2}}"
    {{rec.popupEntry}}
</a>
Sign up to request clarification or add additional context in comments.

6 Comments

@PankajParkar - Yes I did. Correct me if I am wrong.
What if I need the double quote for the function, in another words, I want to the function call to be like followSession("12")? Thanks.
You can update ng-click="followSession($index)" to ng-click="followSession($index+'')"
Thanks @nikhil. My root problem is, I want to pass a string (with spaces) to function followSession.
I think such kind of manipulations should be done in your function.
|
1

You can't use {{}} inside an ng-click. That is the cause of the parse error. Just remove them and it should work. The ng-repeat makes x available on scope. You don't have to wrap it in {{}} in this case.

1 Comment

Thanks @lwalden. Thanks for pointing out my problem with {{}}!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.