简体   繁体   English

ng-repeat中的AngularJS指令范围绑定问题

[英]AngularJS Directive scope binding issue in ng-repeat

am having a directive which is a stopwatch. 我的指令是秒表。 It is placed under ng-repeat so the user can create as many stopwatches he needs. 它放置在ng-repeat下,因此用户可以创建所需的任意秒表。 All these stopwatch has their own play/pause and stop button. 所有这些秒表都有自己的播放/暂停和停止按钮。 The issue am facing is when the user starts the first stopwatch all the other watches starts running, but when starting from the last watch it works fine. 面临的问题是,当用户启动第一个秒表时,所有其他手表都开始运行,但是从最后一个表开始时,它运行正常。 What am I doing wrong here. 我在这里做错了。 Find the plunker 找到the子

my link function: Complete working code is in 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);
  });

},

I see that in your directive template you have an ng-click so this code can be removed 我看到您的指令模板中有一个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);
  });

and then it works :) 然后就可以了:)

working plunkr 工作朋克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM