带有ng-repeat

时间:2016-08-26 21:44:19

标签: javascript angularjs angularjs-directive

我有一个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'
        };
});

0 个答案:

没有答案