指令隔离范围未定义,angularjs 1.3
我在执行指令时,当我在作用域上执行控制台时,我有一个奇怪的行为,我从打印的控制器作用域中得到一个值,但是当我尝试从作用域访问它时,它是未定义的,如作用域。model 代码如下:指令隔离范围未定义,angularjs 1.3,angularjs,angular-directive,Angularjs,Angular Directive,我在执行指令时,当我在作用域上执行控制台时,我有一个奇怪的行为,我从打印的控制器作用域中得到一个值,但是当我尝试从作用域访问它时,它是未定义的,如作用域。model 代码如下: function() { var color = d3.interpolateRgb('#f77', '#77f'); return { template: '<div></div>', restrict: 'E'
function() {
var color = d3.interpolateRgb('#f77', '#77f');
return {
template: '<div></div>',
restrict: 'E',
scope:{
model:'='
},
link: function postLink(scope, element, attrs) {
console.log(scope); //print model
console.log('scope.model ',scope.model); // undefined
var width = attrs.width || 940,
height = attrs.height || 500,
margin = 20;
var modeler = d3.select(element[0])
.append('svg')
.attr('width', width)
.attr('height', height);
}
};
}
function(){
var color=d3.interpolateRgb('f77','77f');
返回{
模板:“”,
限制:'E',
范围:{
型号:'='
},
链接:函数postLink(范围、元素、属性){
console.log(范围);//打印模型
console.log('scope.model',scope.model);//未定义
变量宽度=属性宽度| | 940,
高度=属性高度| | 500,
保证金=20;
var modeler=d3.select(元素[0])
.append('svg')
.attr('width',width)
.attr('高度'),高度;
}
};
}
HTML
<d3-directive model="model" width="920" height="510" ></d3-directive>
请查看控制台输出:
我最后添加了作用域的$watchover值,并使用分配的新值
return {
template: '<div></div>',
restrict: 'E',
scope:{
model:'='
},
link: function postLink(scope, element, attrs) {
console.log(scope);
console.log('scope.model ',scope.model);
scope.$watch('model',function(newValue,oldValue){
if(!!newValue){
console.log(newValue);
}
});
var width = attrs.width || 940,
height = attrs.height || 500,
margin = 20;
var modeler = d3.select(element[0])
.append('svg')
.attr('width', width)
.attr('height', height);
}
};
返回{
模板:“”,
限制:'E',
范围:{
型号:'='
},
链接:函数postLink(范围、元素、属性){
console.log(范围);
日志('scope.model',scope.model);
范围:$watch('model',函数(newValue,oldValue){
如果(!!newValue){
console.log(newValue);
}
});
变量宽度=属性宽度| | 940,
高度=属性高度| | 500,
保证金=20;
var modeler=d3.select(元素[0])
.append('svg')
.attr('width',width)
.attr('高度'),高度;
}
};
有完全相同的问题。似乎使用$watch是唯一的选择