2016-08-29 4 views
0

내 앱에서 특정 값을 확인하면서 이벤트가 boradcasting입니다. 그것은 잘 작동하지만 문제는 나중에 때마다 내 broadcast, 여전히 내 조건이 작동하는 방아쇠를 당하고있다, 내 조건은 항상 trigger happend 후 일하고 있다는 것을 의미합니다. 당신이 논리를 전환 할 경우

var myWatch = scope.$watch('ctrl.data.deviceCity', function(){ 
      if(someCondition === true){ 
       myWatch(); //deregister the watcher by calling its reference 
      } 
    }); 

, 그냥 몇 가지를 설정 : 당신이 그것을 호출 다음 변수에 해당 참조를 저장하고하여 감시자 등록을 취소 할 수 있습니다

scope.$watch('ctrl.data.deviceCity', function(newcity, oldcity) { 

    if (!newcity) { 
     scope.preloadMsg = false; 
     return; 
    } 

    scope.$on('cfpLoadingBar:started', function() { 
     $timeout(function() { 
      if (newcity && newcity.originalObject.stateId) { //the condition not works after the first time means alwasy appends the text 
       console.log('each time'); 

       $('#loading-bar-spinner').find('.spinner-icon span') 
        .text('Finding install sites...'); 
      } 
     }, 100); 
    }); 
}); 
+0

설명에 아무 것도없는 것 같습니다. 표시하는 코드로 수행하십시오. 일어나는 일에 대해 좀 더 자세히 설명해 주실 수 있습니까? 또한 ctrl.data.deviceCity가 변경 될 때마다 'cfpLoadingBar : started'에 대한 새 eventListener가 만들어집니다. 그래서'deviceCity'를 10 번 변경 한 후 갑자기 console.log를 실행하고 그 스피너의 텍스트를 변경하면 10 달러의 시간 초과가 발생한다고 상상해보십시오. – vileRaisin

+0

기본적으로 텍스트를 한 번만 표시해야합니다. 그것은 도시가 변할 때를 의미합니다. 그 후, 나는 그 본문을 보여줄 필요가 없다. 나는 그것을 제거 할 것을 요구할 것입니다 – 3gwebtrain

답변

1

: 여기

내 코드입니다 변수의 어딘가에서 메소드의 제어 흐름을 지시합니다.

var myWatch = scope.$watch('ctrl.data.deviceCity', function(){ 
    scope.calledOnce = false; 

    if(!scope.calledOnce){ 
     //... run this the first time 
     scope.calledOnce = true; 
    } 
    else { 
     // run this the second time (and every other time if you do not deregister this watch or change the variable) 
     // if you don't need this $watch anymore afterwards, just deregister it like so: 
     myWatch(); 
    } 
}) 
+0

좋은 생각, 저를 시도하게하십시오. – 3gwebtrain

관련 문제