1

html代码:

<div class="test">{{name}}</div>

角代码:

var app = angular.module('angularjs-starter', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
});

app.directive('test', function(){
  return {
    restrict: 'C',
    link: function(scope, elm, attrs){
      var content = elm.html();
      alert(content);
    }
  }
});

它会提醒一个字符串{{name}}。如何提醒渲染的字符串World

现场演示:http ://plnkr.co/edit/Mov0AlkdE9B8yKiBjpnp?p=preview

4

1 回答 1

5

您需要使用$interpolate服务来执行此操作

app.directive('test', function($interpolate){
  return {
    restrict: 'C',
    link: function(scope, elm, attrs){
      var content = elm.html();
      alert($interpolate(content)(scope))
    }
  }
});

演示:小提琴

于 2013-03-28T03:30:49.867 回答