I'm trying to use prettyprint with some ajax data. The problem is that when the directive is invoked, the data arem't ready, so I get undefined variable output.
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))
}
};
});
Use scope
's $watch
method:
scope.$watch("result" , function(n,o){
element.html(prettyPrintOne(scope.result));
})
And instead of this:
$scope.result = $http.get('data.json').success(function(result){
return result.data.dom
})
Use this:
$http.get('data.json').success(function(result){
$scope.result = result.dom;
})
Plunk: http://plnkr.co/edit/Autg0V
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With