我正在尝试使用带有一些ajax数据的prettyprint.问题是,当调用指令时,数据没有准备好,所以我得到未定义的变量输出.
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))
}
};
});
Run Code Online (Sandbox Code Playgroud)
使用scope的$watch方法:
scope.$watch("result" , function(n,o){
element.html(prettyPrintOne(scope.result));
})
Run Code Online (Sandbox Code Playgroud)
而不是这个:
$scope.result = $http.get('data.json').success(function(result){
return result.data.dom
})
Run Code Online (Sandbox Code Playgroud)
用这个:
$http.get('data.json').success(function(result){
$scope.result = result.dom;
})
Run Code Online (Sandbox Code Playgroud)
Plunk:http://plnkr.co/edit/Autg0V
| 归档时间: |
|
| 查看次数: |
6097 次 |
| 最近记录: |