5

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))
    }
};
});

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.