【发布时间】:2017-12-12 00:02:13
【问题描述】:
我想要一个与对象列表一起使用的指令,但我还需要接受 2 路绑定或函数绑定。
app.directive('myDir', function() {
return {
scope: {
list: "=?",
list_func: "&?listFunc"
},
controller: ['$scope', function($scope) {
$scope.get_list = function() {
if($scope.list !== undefined)
return $scope.list();
if($scope.list_func !== undefined)
return $scope.list_func()();
};
}]
};
});
但是,当我将 listFunc 属性与返回列表的函数一起使用时,我收到此错误:
VM607 angular.js:68 未捕获的错误:[$rootScope:infdig] 达到了 10 个 $digest() 迭代。中止! 观察者在最近 5 次迭代中触发:[[{"msg":"fn:regularInterceptedExpression","newVal":19,"oldVal":17},{"msg":"fn:regularInterceptedExpression","newVal":"哈利"},{"msg":"fn:regularInterceptedExpression","newVal":"65"},{"msg":"fn:regularInterceptedExpression","newVal":"Sally"},{"msg":" fn:regularInterceptedExpression","newVal":"66"},{"msg":"fn:regularInterceptedExpression","newVal":9,"oldVal":8},{"msg":"fn:regularInterceptedExpression"," newVal":"val"},{"msg":"fn:regularInterceptedExpression","newVal":"1"}],[{"msg":"fn:regularInterceptedExpression","newVal":21,"oldVal" :19},{"msg":"fn:regularInterceptedExpression","newVal":"Harry"},{"msg":"fn:regularInterceptedExpression","newVal":"65"},{"msg":" fn:regularInterceptedExpression","newVal":"Sally"},{"msg":"fn:regularInterceptedExpression","newVal":"66"},{"msg":"fn:regularInterceptedExpression","newVal":10 ,"oldVal":9},{"msg":"fn:regularInterceptedExpression","newVal":"val"},{"msg":"fn:regularInterceptedExpression","newVal" :"1"}],[{"msg":"fn:regularInterceptedExpression","newVal":23,"oldVal":21},{"msg":"fn:regularInterceptedExpression","newVal":"Harry" },{"msg":"fn:regularInterceptedExpression","newVal":"65"},{"msg":"fn:regularInterceptedExpression","newVal":"Sally"},{"msg":"fn: regularInterceptedExpression","newVal":"66"},{"msg":"fn:regularInterceptedExpression","newVal":11,"oldVal":10},{"msg":"fn:regularInterceptedExpression","newVal" :"val"},{"msg":"fn:regularInterceptedExpression","newVal":"1"}],[{"msg":"fn:regularInterceptedExpression","newVal":25,"oldVal":23 },{"msg":"fn:regularInterceptedExpression","newVal":"Harry"},{"msg":"fn:regularInterceptedExpression","newVal":"65"},{"msg":"fn: regularInterceptedExpression","newVal":"Sally"},{"msg":"fn:regularInterceptedExpression","newVal":"66"},{"msg":"fn:regularInterceptedExpression","newVal":12," oldVal":11},{"msg":"fn:regularInterceptedExpression","newVal":"val"},{"msg":"fn:regularInterceptedExpression","newVal":"1"}],[{" msg":"fn: 常规间ceptedExpression","newVal":27,"oldVal":25},{"msg":"fn:regularInterceptedExpression","newVal":"Harry"},{"msg":"fn:regularInterceptedExpression","newVal" :"65"},{"msg":"fn:regularInterceptedExpression","newVal":"Sally"},{"msg":"fn:regularInterceptedExpression","newVal":"66"},{"msg" :"fn:regularInterceptedExpression","newVal":13,"oldVal":12},{"msg":"fn:regularInterceptedExpression","newVal":"val"},{"msg":"fn:regularInterceptedExpression" ,"newVal":"1"}]] http://errors.angularjs.org/1.6.2/$rootScope/infdig?p0=10&p1=%5B%5B%7B%22ms…2fn%3A%20regularInterceptedExpression%22%2C%22newVal%22%3A%221%22%7D%5D%5D 在 VM607 angular.js:68 在 Scope.$digest (VM607 angular.js:17893) 在 Scope.$apply (VM607 angular.js:18125) 完成(VM607 angular.js:12233) 在 completeRequest (VM607 angular.js:12459) 在 XMLHttpRequest.requestLoaded (VM607 angular.js:12387)
我创建了 this plunker 示例(打开一个 javascript 控制台,您可以看到这些错误)演示我需要什么。在此示例中,我收到了这些错误,但视图仍会更新。在我正在开发的应用程序(更大)上,我收到很多$digest 错误,导致网站速度变慢并且视图没有更新。
在不以无限循环结束的情况下绑定函数的最佳方法是什么?
【问题讨论】:
-
表达式
&绑定只能用于将事件传递给父控制器。使用一种方式<从父控制器获取数据。避免双向=绑定。 -
@georgeawg 感谢您的评论。我尽可能避免双向绑定,在我的例子中,输入列表会根据用户与指令的交互而略有变化。
标签: angularjs angularjs-directive