gpt4 book ai didi

javascript - 如何在angularjs中动态附加指令到div

转载 作者:行者123 更新时间:2023-11-28 06:50:35 24 4
gpt4 key购买 nike

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 方法是在 Controller 中使用 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>

关于javascript - 如何在angularjs中动态附加指令到div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32987611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com