如何根据角度js指令限制得到绑定的值:’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}}' } }; });
解决方法
您可以使用$interpolate服务,例如
.directive('logContent',function($log,$interpolate) { return { restrict: 'A',link: function postLink(scope,element) { $log.debug($interpolate(element.text())(scope)); } }; });