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

Mic*_*les 4 angularjs

如何根据角度js指令获得绑定的值restrict: 'A'?

<span directiverestrict> {{binding}} </span>
Run Code Online (Sandbox Code Playgroud)

我尝试使用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}}'
        }
    };
});
Run Code Online (Sandbox Code Playgroud)

Phi*_*hil 6

您可以使用该$interpolate服务,例如

.directive('logContent', function($log, $interpolate) {
  return {
    restrict: 'A',
    link: function postLink(scope, element) {
      $log.debug($interpolate(element.text())(scope));
    }
  };
});
Run Code Online (Sandbox Code Playgroud)

Plunker


Are*_*mar 2

 <span directiverestrict bind-value="binding"> {{binding}} </span>
Run Code Online (Sandbox Code Playgroud)

脚本

directive("directiverestrict", function () {
   return {
           restrict : "A",
           scope : {
                      value : '=bindValue'
                   },
           link : function (scope,ele,attr) {
                alert(scope.value); 
              }
      }
});
Run Code Online (Sandbox Code Playgroud)