簡體   English   中英

如何在angularjs中將指令動態附加到div

[英]how to dynamically append directive to div in angularjs

var myApp = angular.module('myApp', []);

myApp.controller('someController', function($scope) {
    $scope.click = function(){
        alert("asd");
    };

});
myApp.directive('testInput',function(){
     return {
          restrict: 'E',
          replace: false,
          transclude: false,
          template: '<div>asdasdasdasd</div>',
          controller: function($scope) {

         }
      };
}); 

HTML:

<div ng-app = "myApp" ng-controller = "someController">

    <div class = "clickme" ng-click ="click()">
        click me
    </div>

    <div id="container">
    </div>

</div>

如果不使用Jquery,是否有任何良好的Angular方法可將偽指令(testInput)附加到#container? 參見下面的jsfiddle鏈接http://jsfiddle.net/4L6qbpoy/1/

更多的Angular方法是在控制器中將ngIf / ngShow與某些標志一起使用:

 var myApp = angular.module('myApp', []); myApp.controller('someController', function($scope) { $scope.click = function() { $scope.test = true; }; }); myApp.directive('testInput', function() { return { restrict: 'E', replace: false, transclude: false, template: '<div>asdasdasdasd</div>', controller: function($scope) {} }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script> <div ng-app="myApp" ng-controller="someController"> <div class="clickme" ng-click="click()">click me</div> <div id="container"> <test-input ng-if="test"></test-input> </div> </div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM