我有一个html页面,其中包含问题和功能,可以使用选项和代码添加多个选项,并且可以添加或删除选项。
我需要验证每个代码的选项代码在表单
中是唯一的我想在同一个指令中验证代码。我很困惑如何验证我的指令有ng-repeat
的这种字段以下是Plnk
的链接var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.question = { question: 'what is your name',
options: [{option:'Red', code: '1'}, {option:'Blue', code: '2'},
{option:'Green', code: '3'},{option:'Black', code: '4'}]};
});
app.directive('myDirective', function(){
return {
link: function(scope, element, attrs, ctrls) {
function Option() {
this.option = '';
this.code = '';
}
scope.addOption = function(index) {
var existingOption = scope.question.options;
if (existingOption && existingOption.length) {
var newOption = new Option(index + 1);
scope.question.options.splice(index + 1, 0, newOption);
}
}
scope.removeOption = function (index) {
if (index > -1) {
scope.question.options.splice(index, 1);
}
}
},
restrict: 'AE',
scope: {
question: "="
},
templateUrl: 'question.html'
};
});