【问题标题】:ng-repeat do not evaluate from $sce.trustAsHtml inside directiveng-repeat 不从 $sce.trustAsHtml 内部指令评估
【发布时间】:2014-07-25 09:07:17
【问题描述】:

我有一个指令,其中包含一段自定义 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

【问题讨论】:

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


    【解决方案1】:

    您需要确保 $sce 在您的控制器中

    看看这个 -- http://jsfiddle.net/3J25M/2/

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

    另外,看看这个问题:AngularJS using $sce.trustAsHtml with ng-repeat

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      • 2016-02-21
      相关资源
      最近更新 更多