请检查这个小提琴:http: //jsfiddle.net/kgXRa/
这是代码(在 JSFiddle 中实现)
var app = angular.module('myApp', []);
app.controller('MyCtrl', ['$scope', function ($scope) {
$scope.field = [];
$scope.value = [];
$scope.inputCounter = 0;
}]);
app.directive('addInput', ['$compile', function ($compile) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
element.find('button').bind('click', function () {
var input = angular.element('<div><input type="text" ng-model="field[' + scope.inputCounter + ']"><input type="text" ng-model="value[' + scope.inputCounter + ']"> <a href="javascript:;" ng-click="remove_it(' + scope.inputCounter + ')">remove</a></div> ');
var compile = $compile(input)(scope);
element.append(input);
scope.inputCounter++;
scope.remove_it = function(scope_counter) {
compile.remove(this);
scope.inputCounter--;
}
});
}
}
}]);
当用户创建新字段时,我需要添加删除按钮。不知何故,我需要删除计数器,以便在用户删除输入时清除数组。我曾尝试使用 jQuery,但我认为 Angular.JS 中应该有一种方法
使用我编写的脚本删除错误的脚本。
多谢你们。