如何根据角度js指令restrict: 'A'
得到绑定的值?
<span directiverestrict> {{binding}} </span>
我尝试使用elem[0].innerText
,但它返回的是确切的绑定'{{binding}}'
而不是绑定的值
.directive('directiverestrict',function() {
return {
restrict:'A',
link: function(scope, elem, attr) {
// I want to get the value of the binding enclosed in the elements directive without ngModels
console.log(elem[0].textContent) //----> returns '{{binding}}'
}
};
});
答案 0 :(得分:6)
您可以使用$interpolate
服务,例如
.directive('logContent', function($log, $interpolate) {
return {
restrict: 'A',
link: function postLink(scope, element) {
$log.debug($interpolate(element.text())(scope));
}
};
});
答案 1 :(得分:2)
<span directiverestrict bind-value="binding"> {{binding}} </span>
SCRIPT
directive("directiverestrict", function () {
return {
restrict : "A",
scope : {
value : '=bindValue'
},
link : function (scope,ele,attr) {
alert(scope.value);
}
}
});
答案 2 :(得分:2)
在链接阶段,不评估内部绑定,这里最简单的方法是使用$timeout
服务将内部内容的评估延迟到下一个摘要周期,例如
$timeout(function() {
console.log(elem[0].textContent);
},0);