我有一个问题,代码:
html:
<div class="overflow-hidden ag-center" world-data info="target"></div>
JS:
.directive('worldData', ['$interval', function($interval) {
return {
scope: {
chart: '=info'
},
template: '<div>{{chart.aaa}}</div>',
link: function($scope, element, attrs) {
$scope.target = {'aaa': 'aaa'};
aaa = $scope.chart;
}
}
}])
图表值是未定义的,模板没有值,但是当我声明$ scope.target时,代码可以工作,为什么?
这通常应该是模式:
.controller('myController', function($scope){
$scope.target = {'aaa': 'aaa'}; //In reality, you'd normally load this up via some other method, like $http.
})
.directive('worldData', [function() {
return {
scope: {
chart: '=info'
},
template: '<div>{{chart.aaa}}</div>'
}
}])
-
<div ng-controller="myController">
<div class="overflow-hidden ag-center" world-data info="target"></div>
</div>
另外,该指令可能负责获取和获取数据,而不会传递任何内容。您只想考虑如果您不需要多个位置的数据。