【问题标题】:Data-binding arguments of a function with AngularJS in ng-click在 ng-click 中使用 AngularJS 的函数的数据绑定参数
【发布时间】:2020-01-10 02:36:37
【问题描述】:

我正在使用 ng-click 调用带有从 $scope 获得的参数的函数。不幸的是,要么不是从角度处理的参数,要么我得到这个错误:

错误:[$parse:syntax] 语法错误:标记 ':' 不是从 [:notification] 开始的表达式 [:notification] 的第 1 列的主表达式。

导致错误的HTML sn-p:

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

HTML sn-p 未从 Angular 处理:

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

重要提示:notification 是从重复中解析的

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

【问题讨论】:

  • 第二个肯定可以工作..确保您将 goToNotif 定义为 $scope.goToNotif=function(){}
  • 不要使用表达式语法{{ }}作为作用域函数的参数

标签: angularjs function data-binding angularjs-ng-repeat angularjs-ng-click


【解决方案1】:

这里是index.html的代码,单独定义“通知”-

<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}} ).....除了这部分 - 正在把我的头撞到桌子上...... ....谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-16
  • 2018-07-21
  • 1970-01-01
  • 2017-06-18
  • 2014-01-14
  • 1970-01-01
相关资源
最近更新 更多