javascript - 如何从 Angular Controller 将指令插入到 View 中?

标签 javascript asp.net angularjs

我的 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/

相关文章:

angularjs - Visual Studio Code 中的 Angular 2/TypeScript 多行字符串

angularjs - 如何在angularJs中使用jqlite选择具有特定类名的子元素?

JavaScript 安全字符串

javascript - 将键盘绑定(bind)到左/右导航

c# - 从此方法返回的最佳对象 asp.net csharp

asp.net - 从代码隐藏扩展 ASP.Net TreeView 节点

javascript - Angular JS : processing promises in order (without chaining)

javascript - 实现 Rails Frontend 轮询来调用更新

javascript - 单击日期选择器外部的已知容器时,有没有一种方法可以防止剑道日期选择器日历弹出窗口关闭?

asp.net - Server.TransferRequest 不保留 HttpContext.Items