1
我似乎無法弄清楚爲什麼ng-click
指令不適用於複製的DOM元素。讓ngClick在動態字段上工作
這裏是小提琴: http://jsfiddle.net/BkRqa/4/
我可以點擊+ Insert new fieldset
,但按鍵刪除添加的字段集不工作。
HTML
<div ng-app="myApp">
<div ng-controller="myController">
<div id="education">
<button class="add-button" ng-click="cloneField($event)">+ Insert new field</button>
<!-- I want to make copies of .control-fieldset -->
<div class="control-fieldset">
<div class="control-group">
<label for="name_0">School</label>
<input type="text" name="name[0]" id="name_0">
<!-- I want to remove copies of .control-fieldset -->
<button class="remove-button" ng-click="removeField($event)">-</button>
</div>
</div>
</div>
</div>
</div>
控制器
(function() {
var myApp = angular.module('myApp', []);
myApp.controller('myController', ['$scope', function ($scope) {
$scope.cloneField = function (event) {
var fieldHtml = $(event.target).parent().find(".control-fieldset").first().clone();
$(event.target).parent().append(fieldHtml);
};
$scope.removeField = function (event) {
$(event.target).remove();
}
}]);
})();
非常感謝:)一個教訓。 –