【发布时间】:2016-01-07 18:57:41
【问题描述】:
我花了一段时间试图找到一个优雅的解决方案,虽然我有一个“有效”的解决方案,但感觉并不是最简单或正确的做事方式。
所以,我的问题是……如何动态加载指令!在某些情况下,以下是我希望我能够摆脱它的方式!除了加载模板之外,我没有包含路由或其他任何内容,并且我为下面的控制器分配了 ng-controller。
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
<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>
任何人都可以提供的任何建议将不胜感激,如果我做了任何明显愚蠢的事情,请原谅这是我第一次使用 Angular!
【问题讨论】:
-
你需要再写一个辅助指令。
-
@dfsq 别让我挂了 :) 哈哈 有什么线索或文档我可以阅读以了解这一点吗?
-
我猜你的意思是类似于这个答案 - stackoverflow.com/questions/28414568/…
-
可以是这样的,是的。
标签: javascript angularjs