我有一个指令,它是秒表。它被放置在 ng-repeat 下,以便用户可以创建他需要的任意数量的秒表。所有这些秒表都有自己的播放/暂停和停止按钮。 我面临的问题是,当用户启动第一个秒表时,所有其他 watch 都开始运行,但是当从最后一个 watch 启动时,它工作正常。我在这里做错了什么。 Find the plunker
我的链接功能:完整的工作代码在 plunker 中
link: function(scope, element, attrs, ctrl) {
var start_button = angular.element(document.querySelector('.play'));
var pause_button = angular.element(document.querySelector('.pause'));
var stop_button = angular.element(document.querySelector('.stop'));
start_button.on('click', ctrl.start);
pause_button.on('click', ctrl.pause);
stop_button.on('click', ctrl.stop);
scope.$on('$destroy', function () {
start_button.off('click', ctrl.start);
pause_button.off('click', ctrl.pause);
stop_button.off('click', ctrl.stop);
});
},
最佳答案
我发现在您的指令模板中您有一个 ng-click,因此可以删除此代码
var start_button = angular.element(document.querySelector('.play'));
var pause_button = angular.element(document.querySelector('.pause'));
var stop_button = angular.element(document.querySelector('.stop'));
start_button.on('click', ctrl.start);
pause_button.on('click', ctrl.pause);
stop_button.on('click', ctrl.stop);
scope.$on('$destroy', function () {
start_button.off('click', ctrl.start);
pause_button.off('click', ctrl.pause);
stop_button.off('click', ctrl.stop);
});
然后就可以了:)
关于javascript - ng-repeat 中的 AngularJS 指令范围绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29340263/