Data-binding arguments of a function with AngularJS in ng-click
Asked Answered
F

1

5

I am using ng-click to call a function with arguments I get from the $scope. Unfortunately either are the arguments not processed from angular or I get this error:

Error: [$parse:syntax] Syntax Error: Token ':' not a primary expression at column 1 of the expression [:notification] starting at [:notification].

HTML snippet that results in error:

<div ng-click="goToNotif({{notification.id}})"></div>

HTML snippet not being processed from angular:

<div ng-click="goToNotif(notification.id)"></div>

IMPORTANT: notification is parsed from a repeat

<div(ng-repeat="notification in notifications")></div>
Fearless answered 7/6, 2014 at 20:36 Comment(2)
the second one should definitely work ..make sure you defined goToNotif as $scope.goToNotif=function(){}Robey
don't use expression syntax {{ }}for arguments of a scoped functionGoggler
P
9

Here is the code for index.html, define "notifications" seperately -

<div ng-app="MyApp">
    <div ng-controller="MainCtrl">
    <div(ng-repeat="notification in notifications")>
        <div ng-click="go(notification.id)"></div>
    </div>
</div>
</div>

In main.js -

var app = angular.module('MyApp', []);

app.controller('MainCtrl', function($scope) {
$scope.go = function() {
//write code here.
}
});
Palmerpalmerston answered 8/6, 2014 at 9:42 Comment(1)
I was trying to do this inside a directive template and everything else uses the binding syntax ( {{id}} ).....EXCEPT this part - was banging my head on the desk......thanks!Cuspidate

© 2022 - 2024 — McMap. All rights reserved.