我有指令:
angular.module('mymodule').directive('someName', ['$http', function($http) {
return {
restrict: 'AE',
scope: {
headerId: '=header',
data: '=data'
},
templateUrl: 'path/to/template.html',
compile: function() {
return function($scope, $link, $attr) {
console.log($scope.headerId); // print actual header id
console.log($scope.data); // undefined
/* ... code ... */
重点是 header 被硬编码为指令属性中的数字。一切都很好。但数据是通过 Angular 服务异步从服务器获取的。因此,当编译函数运行时,$scope 中没有数据。我的问题是我想更改数据,以便指令模板可以读取它。
我该怎么做?
最佳答案
向数据添加观察者 范围.$watch('data', function() {...})
关于javascript - 更改 Angular Directive(指令)中的异步传入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26762948/