ng-repeat不从$ sce.trustAsHtml inside指令中进行评估

时间:2014-07-25 09:07:18

标签: angularjs angularjs-scope angularjs-ng-repeat

我有一个指令,它有一个自定义HTML,我可以作为自定义选项传入。我想在自定义HTML中使用ng-repeat,但它没有显示。这是代码。

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.local = {
    options: {
      columns: [{
        name: 'product_id',
        label: 'Product ID'
      }, {
        name: 'product_name',
        label: 'Name'
      }],
      getBody: function( col, i ) {
        return col.name == 'product_id' ? col.label : '<div class="dropdown product-status"><button class="btn btn-primary dropdown-toggle" data-toggle="dropdown">Status<span class="caret"></span></button><span class="dropdown-arrow"></span><ul class="dropdown-menu"><li ng-repeat="one_status in dataset.status"><a class="status all" ng-click="dataset.updateStatus(one_status.status_id)" ng-bind="one_status.status_name"></a></li></ul></div>';
      }
    },
    dataset: {
      status: [{
        status_id: 1,
        status_name: 'first draft'
      }, {
        status_id: 2,
        status_name: 'in edit'
      }]
    }
  };
});
app.directive('test', function($sce) {
  var linkFunc = function(scope) {
    scope.getBody = function( col, index ) {
      return $sce.trustAsHtml( scope.options.getBody( col, index ) );
    };
  };

  return {
    link: linkFunc,
    scope: {
      options: '=',
      dataset: '='
    },
    templateUrl: 'test.html'
  };
});

http://plnkr.co/edit/mCynQWtQElTGfoLXZHqZ?p=preview

1 个答案:

答案 0 :(得分:0)

你需要确保$ sce属于你的控制器的一部分

检查一下 - http://jsfiddle.net/3J25M/2/

.controller('ngBindHtmlCtrl', ['$scope','$sce', function ngBindHtmlCtrl($scope, $sce) ...等

另外,请检查此问题:AngularJS using $sce.trustAsHtml with ng-repeat