我的 Angular 模块中有一个自定义指令,我想将其动态插入到我的 View 中,其中多个实例包含来自 Controller 内函数的不同信息,也在模块中。这是如何完成的?例如here is a sample fiddle用于向我展示这是如何完成的
var myApp = angular.module('myApp',[]);
myApp.directive('myDirective', function() {
return {
restrict: "E",
transclude: true,
template: "<div><h4>Hello, World!</h4></div>"
}
});
function MyCtrl($scope) {
$scope.insertDir = function(){
//????
}
}
最佳答案
使用ng-if
。 IE。 <div ng-if="myDirectiveActive"> <my-directive></my-directive> </div>
。它将插入 div
每当 myDirectiveActive
时,将指令放入 DOM 树中标志为真
关于javascript - 如何从 Angular Controller 将指令插入到 View 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26180235/