Nice programing

누군가 AngularJS의 범위에 대해 $ destroy 이벤트의 예를 제공 할 수 있습니까?

nicepro 2020. 11. 14. 11:04
반응형

누군가 AngularJS의 범위에 대해 $ destroy 이벤트의 예를 제공 할 수 있습니까?


누군가 범위의 $ destroy 이벤트의 예를 제공 할 수 있습니까? 다음은 http://docs.angularjs.org/api/ng.$rootScope.Scope#$destroy 의 참조 문서입니다.

$ destroy ()

부모 범위에서 현재 범위 (및 모든 자식)를 제거합니다. 제거는 $ digest ()에 대한 호출이 더 이상 현재 범위와 그 자식으로 전파되지 않음을 의미합니다. 제거는 또한 현재 범위가 가비지 수집에 적합 함을 의미합니다.

$ destroy ()는 일반적으로 ngRepeat와 같은 지시문에서 루프 풀기를 관리하는 데 사용됩니다.

범위가 소멸되기 직전에 $ destroy 이벤트가이 범위에서 브로드 캐스팅됩니다. 애플리케이션 코드는 필요한 정리를 수행 할 수있는 기회를 제공하는 $ destroy 이벤트 핸들러를 등록 할 수 있습니다.


데모 : http://jsfiddle.net/sunnycpp/u4vjR/2/

여기에서 핸들 파괴 지시문을 만들었습니다 .

ctrl.directive('handleDestroy', function() {
    return function(scope, tElement, attributes) {        
        scope.$on('$destroy', function() {
            alert("In destroy of:" + scope.todo.text);
        });
    };
});

$destroy 두 가지를 참조 할 수 있습니다 : 메서드와 이벤트

1. 메소드-$ scope. $ destroy

.directive("colorTag", function(){
  return {
    restrict: "A",
    scope: {
      value: "=colorTag"
    },
    link: function (scope, element, attrs) {
      var colors = new App.Colors();
      element.css("background-color", stringToColor(scope.value));
      element.css("color", contrastColor(scope.value));

      // Destroy scope, because it's no longer needed.
      scope.$destroy();
    }
  };
})

2. 이벤트-$ scope. $ on ( "$ destroy")

@SunnyShah의 대답을 참조하십시오 .

참고 URL : https://stackoverflow.com/questions/14416894/can-someone-provide-an-example-of-a-destroy-event-for-scopes-in-angularjs

반응형