我正在尝试将模型绑定到指令中的属性。
Javascript -
function Main($scope) {
$scope.text = "abc"
};
angular.module("myApp", [])
.directive("load", function ($compile) {
return {
restrict: 'E',
replace: true,
link: function (scope, element, attrs) {
console.log(attrs);
element.html(someFunction(attrs.text));
}
};
});
HTML -
<div ng-controller="Main">
<load text="Hello {{text}}"></load>
</div>
你可以找到jsFiddle here。在小提琴中,我已经废除了someFunction
。
答案 0 :(得分:3)
这是一个快速插件,显示了从指令中获取范围的5种不同方法。最后一个是你想要的那个:http://plnkr.co/edit/e2mMAq
答案 1 :(得分:1)
根据我认为您要做的事情,您需要进行两项修改:
您已将set设置为true,因此您应该在代码中添加模板,以使用新标记替换该元素。
在链接阶段发生时,尚未评估插值,因此您需要观察属性以查找更改。
angular.module('myApp', [])
.directive('load', function($compile) {
return {
restrict: 'E',
replace: true,
link: function (scope, element, attrs) {
console.log(attrs);
element.html(attrs.text);
attrs.$observe('text', function(value) {
console.log('new value = ' + value);
element.html(value);
});
}
};
});
请查看observing interpolated attributes部分了解详情。