使用ng-repeat的动态指令

时间:2021-02-27 19:44:01

I've spent a while trying to find an elegant solution to this, whilst I have a solution that 'works' it doesn't feel like the easiest or correct way of doing things.

我花了一段时间试图找到一个优雅的解决方案,而我有一个“有效”的解决方案,它感觉不是最简单或正确的方法。

So, my question is...how can I dynamically load directives! For some context, below is how I was hoping I'd be able to get away with it! I've not included the routing or anything but the template loads and I assign the below controller with ng-controller.

所以,我的问题是……如何动态加载指令!在某些情况下,下面是我希望我能侥幸逃脱的方法!除了模板加载之外,我没有包含路由或其他内容,我使用ng-controller分配下面的控制器。

app.js

app.js

angular.module('myApp', [])

.controller('someController', ['$scope', function($scope) {
  $scope.directives = ['myDirectiveA', 'myDirectiveB'];
}])

.directive('myDirectiveA', function() {
  return {
    template: '<p>Directive A, exciting.</p>'
  };
})

.directive('myDirectiveB', function() {
  return {
    template: '<p>Directive B, equally as exciting.</p>'
  };
});

template.html

template.html

<div ng-controller="someController">
  <div ng-repeat="directive in directives">

    <x-directive></x-directive> // Attempt 1
    <x-{{directive}}></x-{{directive}}> // Attempt 2
    <{{'x-' + directive}}></{{'x-' + directive}}> // Attempt 3

  </div>
</div>

Any advice that anyone can offer would be greatly appreciated, excuse me if I'm doing anything obviously stupid this is my first time round with Angular!

任何人能提供的任何建议都将非常感谢,如果我做任何明显愚蠢的事情,请原谅我这是我第一次有棱角!

1 个解决方案

#1


2  

i hope this help you:

我希望这能帮助你:

for explain: you have to $compile your directive when you want to use it in other directive like ngRepeat or other custom directive...

解释:当你想在ngRepeat或其他自定义指令等其他指令中使用时,你需要编译你的指令。

        angular.module('myApp', [])

        .controller('someController', ['$scope', function ($scope) {
            $scope.directives = ['my-directive-a', 'my-directive-b'];
        }])

        .directive('directive', function ($compile) {
            return {
                restrict: "A",
                scope: {
                    set: "="
                },
                link: function (scope, element) {
                    element.html("<div class=\" "+ scope.set +" \"></div>");
                    $compile(element.contents())(scope);
                }
            };
        })

        .directive('myDirectiveA', function () {
            return {
                restrict: "C",
                template: '<p>Directive A, exciting.</p>'
            };
        })

        .directive('myDirectiveB', function () {
            return {
                restrict: "C",
                template: '<p>Directive B, equally as exciting.</p>'
            };
        });
<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <title></title>
</head>
<body ng-controller="someController">
    
    <div ng-repeat="directive in directives">
        <div directive set="directive"></div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>
</body>
</html>

#1


2  

i hope this help you:

我希望这能帮助你:

for explain: you have to $compile your directive when you want to use it in other directive like ngRepeat or other custom directive...

解释:当你想在ngRepeat或其他自定义指令等其他指令中使用时,你需要编译你的指令。

        angular.module('myApp', [])

        .controller('someController', ['$scope', function ($scope) {
            $scope.directives = ['my-directive-a', 'my-directive-b'];
        }])

        .directive('directive', function ($compile) {
            return {
                restrict: "A",
                scope: {
                    set: "="
                },
                link: function (scope, element) {
                    element.html("<div class=\" "+ scope.set +" \"></div>");
                    $compile(element.contents())(scope);
                }
            };
        })

        .directive('myDirectiveA', function () {
            return {
                restrict: "C",
                template: '<p>Directive A, exciting.</p>'
            };
        })

        .directive('myDirectiveB', function () {
            return {
                restrict: "C",
                template: '<p>Directive B, equally as exciting.</p>'
            };
        });
<!DOCTYPE html>
<html ng-app="myApp">
<head>
    <title></title>
</head>
<body ng-controller="someController">
    
    <div ng-repeat="directive in directives">
        <div directive set="directive"></div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script>
</body>
</html>