我正在使用ScrollSpy的AngularJS实现中的代码(在此处原始文章),但是在动态创建导航时遇到了问题,但是在静态创建导航时它确实起作用。
所以我有一个scrollSpy
指令,监视一个列表spies
。该spies
列表基本上是在用户滚动浏览页面时应突出显示的导航元素的列表。spies
通过控制器中的addSpy
方法添加,scrollSpy
就像这样
controller: function ($scope) {
$scope.spies = [];
return this.addSpy = function (spyObj) {
return $scope.spies.push(spyObj);
};
},
该addSpy
函数总是被调用,但是当我动态添加间谍时,该列表的$ watch永远不会被触发,当静态创建导航项时,它确实会被触发。
link: function (scope, elem, attrs) {
scope.$watch('spies', function (spies) {
// I never get called when spies are added dynamically, even
// though spies are added to the $scope.spies object in the controller!
}
谁能帮助我了解为什么未启动$ watch?我尝试添加$scope.$apply
它,但是它说它已经在摘要周期内。
scope.$watch('spies', function (spies) {
// I never get called when spies are added dynamically, even
// though spies are added to the $scope.spies object in the controller!
}, true);
最后,您必须坚持,,true
因为您没有更改参考,只是对其进行了更新。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句