Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 在HTML中动态插入角度指令_Angularjs - Fatal编程技术网

Angularjs 在HTML中动态插入角度指令

Angularjs 在HTML中动态插入角度指令,angularjs,Angularjs,我的angular模块中有许多指令,它们被命名为:MyDirective1,MyDirective2,我的指示 我还维护了一个指令名列表:MyDirectiveList:['MyDirective1'、'MyDirective2'、…、'MyDirectiveN'] 在HTML中,我想显示一个基于变量(selectedDirective)的指令,该变量保存指令名(使用ng if或ng show) …指令应该放在这里。。。 我可以在控制器中使用$injector和$compile将其插入元素,

我的angular模块中有许多指令,它们被命名为:MyDirective1,MyDirective2,我的指示

我还维护了一个指令名列表:MyDirectiveList:['MyDirective1'、'MyDirective2'、…、'MyDirectiveN']

在HTML中,我想显示一个基于变量(selectedDirective)的指令,该变量保存指令名(使用ng if或ng show)


…指令应该放在这里。。。

我可以在控制器中使用$injector和$compile将其插入元素,但我想知道是否有更多的角度方法可以通过角度指令直接在html中执行此操作。

因为您正在操作DOM,所以应该使用指令来执行此操作

嗯,你可以回答说ng repeat和ng if也是指令,你是对的。但你现在打电话给军队是因为有人偷了你的钱包

最好的解决办法似乎是提出的办法

此解决方案之所以更好,有两个原因:

1) 从理论上讲,您不再能够仅仅为了显示一个DOM对象而创建和显示一千个DOM对象

2) 如果指令需要,您可以传递自定义的json数据(请注意,您只能通过唯一的json传递数据,而不能适应指令的需要,即具有智能双向数据绑定的多个字段)

<div ng-repeat="directiveName in MyDirectiveList">

   <div ng-if="selectedDirective == directiveName">

   ...directive shall go in here...

   </div>

</div>