我正在做一个指令当我在范围上执行控制台时,我有一个奇怪的行为我从打印的控制器范围传递了一个值,但是当我尝试从范围访问它时未定义,如 scope.model
这是代码:
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);
}
};
}
HTML
<d3-directive model="model" width="920" height="510" ></d3-directive>
请查看控制台输出:
答案 0 :(得分:1)
我最后添加了一个$ watch over the scope的值并使用指定的新值。
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);
}
};