2015-01-23 4 views
1

각도 지시문이 업데이트 될 때마다 함수가 실행되도록하고 싶습니다. 필자의 경우, 모달 마크 업 템플릿에 사용되는 여러 모달 구성이 있습니다. 모델이 변경되어 템플릿이 모달을 생성하는 데 사용될 때마다 positionModal() 메서드를 실행하려고합니다. 범위. 모델을 변경하면 링크 함수의 $ watch가 눈치 채지 않는 것 같습니다. 다른 방법으로 생각할 수 없습니다. 컴파일러 함수가 지시어가 적용될 때 호출 될 것이라고 생각한 후 연결 함수를 시도했지만 그 중 하나도 작동하지 않는 것 같습니다. 여기에 예제 컨트롤러가 있습니다 :각도 : 지시어가 업데이트 될 때마다 메소드 실행

MyApp.controller("ModalController", function() { 

    //Define scope vars 
    $scope.modals = []; 

    $scope.$on("modalTrigger", function (event, settings) { 
     $scope.modals.push(settings); 
    }); 
}); 

참고 : 여기 컨트롤러가 단순화되었으므로 작동하는 것으로 알고 있습니다.

<div class="modalParent" ng-controller="ModalController"> 
    <div id="{{modal.id}}" class="modal" ng-class="modal.type" ng-repeat="modal in modals"> 
     <div class="content"> 
      <h2 ng-show="modal.title">{{modal.title}}</h2> 
      <p>{{modal.message}}</p> 
      <button>{{modal.button}}</button> 
     </div> 
    </div> 
</div> 

이 지침은 다음과 같이 현재 :

MyApp.directive("modalParent", function() { 
    var positionModals = function (element) { 
     element.find(".modal .content").each(function() { 
      //position logic here 
     }); 
    }; 

    return { 
     restrict: "C", 
     compile: function (tElement) { 
      positionModals(tElement); 
     } 
    }; 
}); 

참고 : 또한 여기 목적으로 단순화 여기

템플릿 코드입니다. positionModals() 호출은 첫 번째 모달이 배열로 푸시 될 때 작동합니다. 그 후에는 작동을 멈 춥니 다.

나는 연결 함수를 사용해도 동일한 결과를 시도했습니다. scope. $ watch (modals, function() {...})가 작동하지 않습니다.

누군가 내가 내가 뭘 잘못하고 있는지 알아낼 수 있습니까?

+0

scope.watch는 어디에서 시도 했습니까? html 구조는 어떻게 생겼습니까? 즉'ModalController'와'modal-parent' 사이의 관계입니까? – PSL

+0

ng-controller = modalParent 클래스를 가진 div의 경우 "ModalController". 범위는 $ watch가 링크 함수 – dudewad

+0

에 배치되었습니다. 여기에는 아무 것도 연결되어 있지 않습니다. 간단한 데모에서 복제 할 수 있습니까? – charlietfl

답변

1

알아 냈어! 지시문을 부모 ".modalParent"에 적용하고있었습니다. 이 경우 ng-repeated 요소는 모달 자체 ".modal"입니다. 모델이 변경 될 때 업데이트를받는 요소에서 지시문을 실행해야 할 것입니다. 연결 함수는 부모를보고 앉아서 거기에서 업데이트하려고 시도하기보다는 요소가 인스턴스화 될 때마다 호출되기 때문입니다. 희망이 있으면 도움이됩니다. 대신 다음과 같이 호출

0

, 내 접근 방식은, 서비스에이를 작성하고 그 기능을 얻기 위해 원하는 목적지 컨트롤러에서 서비스 나 데이터로 통지 할 것을 주입하는 것입니다

Services.js

as.service("xyzservice",function(factoryname){ 
//here the code goes... 
}) 

이제 우리는 두 제어기에 주입 한 다음

ac.controller("controllername",function(xyzservice){ 

}) 
ac.controller("controllername",function(servicename){ 

}) 

ac.controller("controllername",function(xyzservice){ 

}) 

제어기에 주입, 우리는 그것을 얻을 수있다.

관련 문제