다음 지시문에서 꽤 오래 실행되는 작업을 수행하므로 그 시간 동안 로딩 스핀을 표시하고 싶습니다. 나는 지시어의 범위에서 ng-show 및 isLoading 변수를 사용했습니다. 그러나 isLoading이 true로 설정되어 있지만 회 전자는 표시되지 않습니다.angularjs : 지시문 내부에서 UI가 업데이트되지 않습니다.
내 코드에 어떤 문제가 있습니까?
angular.module('shared.directives').directive("xmlVisualizer", ['$rootScope', function ($rootScope) {
return {
restrict: 'E',
template: '<div ng-show="isLoading" class="center span10"><i class="icon-spinner icon-6x">chargement du fichier...</i> </div> <div class="center span10" ng-show="!isLoading"> <h4>Détail du fichier {{title}}</h4> <pre id="test" class="prettyprint"></pre></div>',
scope: {
model: '=',
title: '='
},
link: function (scope, element, attrs) {
if (scope.model) {
scope.isLoading = true;
scope.xml = vkbeautify.xml(scope.model);
$("#test").text(scope.xml);
$("#test").html(prettyPrintOne($("#test").html(), 'xml'));
scope.isLoading = false;
}
}
}
}]);
나에게 맞는 것 같지 않습니다. 왜 내가 전체 범위를 볼까요? 그것은 UI를 업데이트하지 않을 것입니다. 나는 scope를 시도했다. $ apply()하지만 소화가 진행 중이므로 이미 작동하지 않았다. – Sam
링크 함수 내부에서 스코프를 명시 적으로 관찰해야합니다. U는 링크 함수의 인수를 로깅하여 호출 횟수를 확인할 수 있습니다. 논리는 범위가 변경 될 때마다 매번 호출되지만 실제로는 DOM의 특정 HTML에서 init 또는 변경시에만 호출됩니다. 범위 변경은 실제로 작성한 링크 기능을 트리거합니다. 또한 scope의 모든 변경은 $ watch 인수 내에서 두 번째 함수를 트리거하지 않습니다. 콘솔 로깅을 통해이 모든 것을 추론했습니다. 100 % 맞지 않을 수도 있습니다. 도움을 구하려고 –
@ 샘 그가 모델 전체를 보지 않습니다. 'function (scope) {return scope.model}'은''model''와 같습니다. – finishingmove