2013-09-23 6 views
5

일부 ajax 데이터와 함께 prettyprint를 사용하려고합니다. 문제는 지시어가 호출 될 때 데이터가 준비되지 않았기 때문에 정의되지 않은 변수 출력을 얻는다는 것입니다.AngularJs 지시어 비동기 데이터보기

Plunkr : http://plnkr.co/edit/fdRi2nIvVzT3Rcy2YIlK?p=preview

var app = angular.module('plunker', []); 

app.controller('MainCtrl', function($scope,$http) { 


$scope.result = $http.get('data.json').success(function(result){ 
    return result.data.dom 
}) 

}); 


app.directive('prettyprint', function() { 
return { 
    restrict: 'C', 
    link: function postLink(scope, element, attrs) { 
      element.html(prettyPrintOne(scope.result)) 
    } 
}; 
}); 

답변

5

사용 scope$watch 방법이의

scope.$watch("result" , function(n,o){ 
     element.html(prettyPrintOne(scope.result)); 
}) 

대신 :

$scope.result = $http.get('data.json').success(function(result){ 
     return result.data.dom 
}) 

사용이 :

$http.get('data.json').success(function(result){ 
     $scope.result = result.dom; 
}) 

쿵하는 소리 : 내가 볼 http://plnkr.co/edit/Autg0V

+0

나는 plunkr를 업데이트했지만, 문제가 해결되지 것 같다 ... – Tropicalista

+0

@Tropicalista. 내 대답을 업데이트 해 봤어. – Cherniv

관련 문제