[英]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: HTML:
<div ng-app = "myApp" ng-controller = "someController">
<div class = "clickme" ng-click ="click()">
click me
</div>
<div id="container">
</div>
</div>
Without using Jquery is there any good Angular way to append directive(testInput) to #container ? 如果不使用Jquery,是否有任何良好的Angular方法可将伪指令(testInput)附加到#container? see below jsfiddle link http://jsfiddle.net/4L6qbpoy/1/
参见下面的jsfiddle链接http://jsfiddle.net/4L6qbpoy/1/
More Angular approach is to use ngIf/ngShow with some flag in controller: 更多的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.