2013-09-23 6 views
5

मैं कुछ AJAX डेटा के साथ सुंदर प्रिंट का उपयोग करने की कोशिश कर रहा हूं। समस्या यह है कि जब निर्देश लागू किया जाता है, तो डेटा तैयार नहीं होता है, इसलिए मुझे अपरिभाषित परिवर्तनीय आउटपुट मिलता है।एंगुलरजेएस निर्देश एसिंक डेटा देख रहे हैं

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

संबंधित मुद्दे