wmi*_*mil 9 javascript angularjs
如果我使用的是隔离范围,我可以在属性上传递变量.
即
<my-directive baz='foo.bar'>
Run Code Online (Sandbox Code Playgroud)
然后,在指令的Javascript上
.directive('myDirective', function() {
return {
scope: {
'baz': '='
}
}
});
Run Code Online (Sandbox Code Playgroud)
有没有办法用继承范围做类似的事情?链接函数只传递字符串.
现在我正在自己解析变量并将其与范围匹配.$ parent.似乎应该有一个帮助函数或更简单的方法来执行它.
Mar*_*cok 14
使用$eval或$parse:
<my-directive baz='foo.bar'>
Run Code Online (Sandbox Code Playgroud)
.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");
}
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4682 次 |
| 最近记录: |