在AngularJS中使用ng-click绑定函数的数据绑定参数

5

我在使用ng-click调用一个函数,并且将参数从$scope中获取。不幸的是,这些参数没有被angular处理,或者我会得到以下错误:

错误:[$parse:syntax] 语法错误:Token ':' 不是表达式的主要部分,位于[:notification]表达式的第1列起始处。

导致错误的HTML片段:

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

Angular无法处理HTML片段:

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

重要提示: notification是从一个重复项中解析出来的

<div(ng-repeat="notification in notifications")></div>

第二个肯定可以工作... 确保你将goToNotif定义为$scope.goToNotif=function(){}。 - Srinath
不要在作用域函数的参数中使用表达式语法“{{}}”。 - charlietfl
1个回答

9
以下是index.html的代码,"notifications"被单独定义 -
<div ng-app="MyApp">
    <div ng-controller="MainCtrl">
    <div(ng-repeat="notification in notifications")>
        <div ng-click="go(notification.id)"></div>
    </div>
</div>
</div>

在 main.js 文件中 -
var app = angular.module('MyApp', []);

app.controller('MainCtrl', function($scope) {
$scope.go = function() {
//write code here.
}
});

我试图在指令模板中做到这一点,而其他所有内容都使用绑定语法({{id}}).....除了这部分 - 我一直在撞头......谢谢! - Jester

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接