2013-07-12 3 views
0

다음 지시문에서 꽤 오래 실행되는 작업을 수행하므로 그 시간 동안 로딩 스핀을 표시하고 싶습니다. 나는 지시어의 범위에서 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; 
     } 
    } 
} 
}]); 

답변

0

는이

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) { 
      scope.$watch(function(scope){return scope.model}, function(model){ 
       if (model) { 
        scope.isLoading = true; 

        scope.xml = vkbeautify.xml(scope.model); 
        $("#test").text(scope.xml); 
        $("#test").html(prettyPrintOne($("#test").html(), 'xml')); 

        scope.isLoading = false; 
       } 
      }) 
     } 
    } 
}]); 

PS를 시도 범위

를 볼 필요 : 나 자신을 아주 잘 각도에 조예가 아닙니다. 희망이 당신을 위해 작동합니다.

+0

나에게 맞는 것 같지 않습니다. 왜 내가 전체 범위를 볼까요? 그것은 UI를 업데이트하지 않을 것입니다. 나는 scope를 시도했다. $ apply()하지만 소화가 진행 중이므로 이미 작동하지 않았다. – Sam

+0

링크 함수 내부에서 스코프를 명시 적으로 관찰해야합니다. U는 링크 함수의 인수를 로깅하여 호출 횟수를 확인할 수 있습니다. 논리는 범위가 변경 될 때마다 매번 호출되지만 실제로는 DOM의 특정 HTML에서 init 또는 변경시에만 호출됩니다. 범위 변경은 실제로 작성한 링크 기능을 트리거합니다. 또한 scope의 모든 변경은 $ watch 인수 내에서 두 번째 함수를 트리거하지 않습니다. 콘솔 로깅을 통해이 모든 것을 추론했습니다. 100 % 맞지 않을 수도 있습니다. 도움을 구하려고 –

+0

@ 샘 그가 모델 전체를 보지 않습니다. 'function (scope) {return scope.model}'은''model''와 같습니다. – finishingmove

1

제가 생각하기에 문제는 실제로 템플릿의 isLoading이 지시문 안에 isLoading이 아니라 오히려 부모 범위에서 찾고 있다고 생각합니다. 따라서 undefined이므로 (undefined은 지시문 내에서 isLoading 만 변경하기 때문에) 항상 !isLoading 블록을 표시합니다.

당신은

template: '<div is-loading="isLoading" 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: '=', 
     isLoading: '=' 
    }, 

을 시도 할 수 있지만 원시와 함께 일하는 것이 있는지 확실하지 않습니다. 그렇지 않다면 객체 $scope.isLoading = {value: true}; (상위 범위 - 아마도 컨트롤러)과 is-loading="isLoading.value" (템플릿)을 시도해보십시오.

그런 식으로 컨트롤러의 스피너 플래그가 양방향 바인딩을 통해 업데이트되고 지시문이 해당 템플릿에서 사용할 수 있습니다.

관련 문제