我似乎无法弄清楚为什么该ng-click
指令不适用于复制的 DOM 元素。
这是小提琴: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();
}
}]);
})();