如果我使用孤立的范围,我可以通过一个属性传递一个变量.
即
<my-directive baz='foo.bar'>
然后,在指令的Javascript
.directive('myDirective',function() {
return {
scope: {
'baz': '='
}
}
});
有没有办法做一些类似于继承范围的事情?链接功能只是传递字符串.
现在我正在解析变量,并将其与范围匹配.$parent.似乎应该有一个帮助功能或更简单的方法来做到这一点.
解决方法
使用$eval或$parse:
<my-directive baz='foo.bar'>
.directive('myDirective',function($parse) {
return {
scope: true,link: function(scope,element,attrs) {
console.log(scope.$eval(attrs.baz));
var model = $parse(attrs.baz);
console.log(model(scope));
// if you want to modify the value,use the model,not $eval:
model.assign(scope,"new value");
}
}
});