【问题标题】:List of directives inside of an ng-repeatng-repeat 中的指令列表
【发布时间】:2015-10-15 21:23:49
【问题描述】:

我正在处理一个由 5 个指令组成的页面,例如:

<directive-one></directive-one>
<directive-two></directive-two>
<directive-three></directive-three>
<directive-four></directive-four>
<directive-five></directive-five>

我希望能够根据需要重新订购这些内容,以便用户可以控制其页面的外观。我能想到的唯一方法是将它们放入 ng-repeat:

$scope.directiveOrder = [{
    name: "directive-one",
    html: $sce.trustAsHtml('<directive-one></directive-one>'),
    order: 1
}, ...

HTML:

<div ng-repeat="directive in directiveOrder" ng-bind-html="directive.html">
    {{directive.html}}
</div>

这会给我正确的标签,但它们不会被 angular 当作指令处理。有办法解决吗?我假设这与 $sce 没有处理它有关,但我可能会走得很远?

【问题讨论】:

标签: javascript angularjs angularjs-directive angularjs-ng-repeat


【解决方案1】:

尝试创建一个新指令并使用 $compile 来渲染每个指令:

https://jsfiddle.net/HB7LU/18670/ http://odetocode.com/blogs/scott/archive/2014/05/07/using-compile-in-angular.aspx

HTML

<div ng-controller="MyCtrl">
    <button ng-click="reOrder()">Re-Order</button>
    <div ng-repeat="d in directives">
        <render template="d.name"></render>
    </div>
</div>

JS

var myApp = angular.module('myApp',[])
.directive('directiveOne', function() {
    return {
        restrict: 'E',
        scope: {},
        template: '<h1>{{obj.title}}</h1>',
        controller: function ($scope) {
            $scope.obj = {title: 'Directive One'};
        }
    }
})
.directive('directiveTwo', function() {
    return {
        restrict: 'E',
        scope: {},
        template: '<h1>{{obj.title}}</h1>',
        controller: function ($scope) {
            $scope.obj = {title: 'Directive Two'};
        }
    }
})
.directive("render", function($compile){
    return {
        restrict: 'E',
        scope: {
            template: '='
        },
        link: function(scope, element){
            var template = '<' + scope.template + '></' + scope.template + '>';
            element.append($compile(template)(scope));
        }
    }
})
.controller('MyCtrl', function($scope, $compile) {
    $scope.directives = [{
        name: 'directive-one'
    }, {
        name: 'directive-two'
    }];
    $scope.reOrder = function () {
        $scope.directives.push($scope.directives.shift());
        console.log($scope.directives);
    };
});

【讨论】:

    【解决方案2】:

    我希望你能轻松做到。

    var myApp = angular.module('myApp',[])
    .directive('directiveOne', function() {
        return {
            restrict: 'E',
            scope: {},
            template: '<h1>{{obj.title}}</h1>',
            controller: function ($scope) {
                $scope.obj = {title: 'Directive One'};
            }
        }
    })
    .directive('directiveTwo', function() {
        return {
            restrict: 'E',
            scope: {},
            template: '<h1>{{obj.title}}</h1>',
            controller: function ($scope) {
                $scope.obj = {title: 'Directive Two'};
            }
        }
    });
    
    myApp.controller('ctrl',function($scope){
        $scope.data = [{name:'directive-one'},{name:'directive-two'}];
    });
    <html ng-app='myApp'>
      <head>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js"></script>
      </head>
      <body ng-controller='ctrl'>
          <div ng-repeat='item in data'>
          <item.name></item.name>
            <directive-one></directive-one>
        </body>
      </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-07
      相关资源
      最近更新 更多