Javascript 角度指令作用域不绑定数据
我有一个问题,代码如下: HTML:Javascript 角度指令作用域不绑定数据,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我有一个问题,代码如下: HTML: js: .directive('worldData',['$interval',function($interval){ 返回{ 范围:{ 图表:'=信息' }, 模板:“{chart.aaa}}”, 链接:函数($scope,element,attrs){ $scope.target={'aaa':'aaa'}; aaa=$scope.chart; } } }]) 图表值未定义,模板没有值,但当我在控制器中声明$scope.target时,代码正常工
js:
.directive('worldData',['$interval',function($interval){
返回{
范围:{
图表:'=信息'
},
模板:“{chart.aaa}}”,
链接:函数($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>'
}
}])
.controller('myController',函数($scope){
$scope.target={'aaa':'aaa'};//实际上,您通常会通过其他方法加载它,比如$http。
})
.directive('worldData',[function(){
返回{
范围:{
图表:'=信息'
},
模板:“{chart.aaa}”
}
}])
--
或者,该指令可以负责获取数据,而不向其传递任何信息。你只想考虑如果你不需要多个地方的数据。你的代码是如何工作的,我希望在指令的父范围内有一个<代码>目标<代码>值。就我所见,link函数没有做任何应该影响呈现值的事情。当我使用它时,
link:function($scope,element,attrs){$scope.target={'aaa':'aaa'};$scope.chart=$scope.target;}
DOM立即呈现。代码运行得非常好,因为您正在指令中重新定义图表<代码>图表:'=info'用于从指令外部传入数据。在这种情况下,您通过映射到指令的$scope.chart
的info
将target
(应该在指令外部定义,可能在控制器中)传递到指令。您是否将target定义为要传入的指令之外的适当数据?否,$scope.target define outside指令起作用,当我定义内部链接函数时,它不起作用,让我感到困惑。这个有棱角的文件什么也没说,看我的答案。隔离作用域作用域:{chart:'=info'}
仅用于通过属性映射将值从指令外部传递到指令内部,在这种情况下,传递到info=”“
的值将映射到指令内部的$scope.chart
。如果要在指令内定义值,请在link
或controller
中执行该操作,并在指令定义中只包含scope:{}
。
.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;
}
}
}])
.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>