각도 지시문이 업데이트 될 때마다 함수가 실행되도록하고 싶습니다. 필자의 경우, 모달 마크 업 템플릿에 사용되는 여러 모달 구성이 있습니다. 모델이 변경되어 템플릿이 모달을 생성하는 데 사용될 때마다 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() {...})가 작동하지 않습니다.
누군가 내가 내가 뭘 잘못하고 있는지 알아낼 수 있습니까?
scope.watch는 어디에서 시도 했습니까? html 구조는 어떻게 생겼습니까? 즉'ModalController'와'modal-parent' 사이의 관계입니까? – PSL
ng-controller = modalParent 클래스를 가진 div의 경우 "ModalController". 범위는 $ watch가 링크 함수 – dudewad
에 배치되었습니다. 여기에는 아무 것도 연결되어 있지 않습니다. 간단한 데모에서 복제 할 수 있습니까? – charlietfl