angularjs指令 – 获取元素绑定的文本内容

前端之家收集整理的这篇文章主要介绍了angularjs指令 – 获取元素绑定的文本内容前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何根据角度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));
    }
  };
});

Plunker

猜你在找的Angularjs相关文章