簡體   English   中英

如何在按鈕單擊時動態添加指令

[英]How to dynamically add directives on button-click

我已經看過同一主題的帖子: 在AngularJS中動態添加指令

但是我對angular不夠熟悉,無法在我的情況下使用此解決方案。

就我而言,我試圖在按鈕單擊后向div動態添加一個指令(基於外部html):

        <div class="charges" id="charges">
            <my-headcharges></my-headCharges>
            <my-charges></my-charges>

        </div>

Nou可以看到我的div,其中包含我創建並放置在div硬編碼下的兩個指令。 現在,我想添加相同的指令,但是多次並單擊按鈕后。

謝謝 :)

您可以使用ng-repeat

 var myApp = angular.module("myApp", []); myApp.controller("myController", ["$scope", function($scope) { $scope.items = [] $scope.addTimes = function() { $scope.items.push({ name : new Array(7).join().split(',').map(function(it){return String.fromCharCode(Math.floor(Math.random() * (122 - 97 + 1)) + 97)}).join("") }); }; } ]); 
 <html ng-app="myApp"> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> </head> <body ng-controller="myController"> <button ng-click="addTimes()">Add</button> <div class="charges" ng-repeat="item in items"> <my-headcharges>Hello {{item.name}}</my-headCharges> <my-charges></my-charges> </div> </body> </html> 

暫無
暫無

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

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