范围。$ on()为一个广播调用几次

时间:2014-08-14 14:46:47

标签: angularjs

在Angular.js中,当scope.$on("UPDATE", function(event, account, vipLabel, nodeName)只应为每个$rootScope.$broadcast("UPDATE", this.account,this.vipLabel, this.nodeName)调用一次时,scope.$on()会触发约20次。我知道scope.$on()包含一个事件对象,但我无法找到它来帮助解决我的问题。

我被困在这上面,我需要angular.module('main.vips') .factory("StatusTrackerService", function($timeout, $rootScope, Restangular) { var CHECK_ITERATIONS = 1; var TIME_ITERATION = 1000; var eventCollection = {}; eventCollection.vipLabel; function urlBuilder(account, eventId) { var account = Restangular.one("account", account); var eventId = account.one("event", eventId); return eventId; } function NewStatusEvent(account, eventId, url, vipLabel, nodeName) { this.iteration = 0; this.account = account; this.eventId = eventId; this.url = url; this.vipLabel = vipLabel; this.nodeName = nodeName; } NewStatusEvent.prototype.runCheckLoop = function() { this.url.get() .then(function(data) { if (data.Automation.Status != "SUCCESS") { console.log("yes"); console.log(this.iteration); console.log(CHECK_ITERATIONS); if (this.iteration < CHECK_ITERATIONS) { this.iteration++; $rootScope.$broadcast("UPDATE", this.account, this.vipLabel, this.nodeName); console.log('-------------------'); console.log('checking ' + JSON.stringify(this.url)); console.log('iteration ' + this.iteration); console.log('-------------------'); $timeout(function (){ this.runCheckLoop(); }.bind(this), TIME_ITERATION); } } }.bind(this)); } function runEventCheck(account, eventId, nodeName) { url = urlBuilder(account, eventId); eventCollection[eventCollection.vipLabel] = new NewStatusEvent(account, eventId, url, eventCollection.vipLabel, nodeName); eventCollection[eventCollection.vipLabel].runCheckLoop(); } return { runEventCheck: runEventCheck, eventCollection: eventCollection }; }); 每次广播只发射一次。

            return {
                templateUrl: "vips/directives/action_button.html",
                restrict: "AE",
                replace: true,
                transclude: false,
                scope: {
                  label: "@?",
                  icon: "@?",
                  type: "@?",
                  actions: "=?"
                },
                link: function(scope, element, attrs) {
                  element.on("click", function(event) {
                      var vipLabel = event.currentTarget.id;
                      StatusTrackerService.eventCollection.vipLabel = vipLabel;
                  });

                  scope.$on("UPDATE", function(event, account,
                              vipLabel, nodeName) {
                      console.log("Scope.on has fired this many times");
                      var nodeSelector = "#node-"+vipLabel+"-"+nodeName;
                      var nodeElement = angular.element(document.querySelector(nodeSelector));
                      if (!nodeElement.length) {
                          var vipElement = angular.element(document.querySelector('#vipLabel-'+vipLabel));
                          var elementGlobe = '<div id="'+vipLabel+nodeName+'">Element modified</div>'; 
                          vipElement.append(elementGlobe);
                      }
                  });

                  return scope.actions = services[attrs.type];
                }
              }
});

指令:

21 -Scope.on has fired this many times actionButton.js:52
------------------- status-checker.js:35
checking {"id":"0fd6afd9-2367-4a0-a5c9-ff0b3e60cdcf","route":"event","reqParams":null,"$fromServer":false,"parentResource":{"route":"account","parentResource":null,"id":"99006"},"restangularCollection":false} status-checker.js:36
iteration 1 status-checker.js:37
------------------- 

记录输出:

<div action-button type="loadbalancer" label="Actions" icon="glyphicon glyphicon-cog"
       actions="actions.loadbalancer"></div>

</div>

              <div id="{{vip.label}}" action-button type="vip" icon="glyphicon glyphicon-pencil" actions="actions.vips"
                   class="pull-right"></div>


                      <div action-button type="node" icon="glyphicon glyphicon-pencil" actions="actions.nodes"
                           class="pull-right"></div>

HTML:

if (scope.type !== "vip")

更新: 我添加了<div id="{{vip.label}}" action-button type="vip"并且它有效......现在事件只触发一次。这对我来说没有意义,因为它是我正在使用的if (scope.type == "vip")。似乎它应该是 scope.$on("UPDATE", function(event, account, vipLabel, nodeName) { if (scope.type !== "vip") { console.log(scope); var nodeSelector = "#node-"+vipLabel+"-"+nodeName; var nodeElement = angular.element(document.querySelector(nodeSelector)); console.log(nodeElement.length); if (nodeElement.length == 0) { console.log("create node"); var vipElement = angular.element(document.querySelector('#vipLabel-'+vipLabel)); var elementGlobe = '<div id="'+vipLabel+nodeName+'">Element modified</div>'; vipElement.append(elementGlobe); } } }); 而不是......但它恰恰相反。

{{1}}

0 个答案:

没有答案