如果已在下拉列表中选择,则禁用所选选项

时间:2017-03-08 06:33:17

标签: angularjs

这是我的json格式数据

 [
{
    "SecurityQuestionID": 10,
    "Question": "Which is your favorite web browser?",
    "IsActive": true,
    "Created": "2017-02-22T05:15:57+00:00",
    "Modified": "2017-02-22T05:15:57+00:00"
},
{
    "SecurityQuestionID": 9,
    "Question": "What is your favorite color?",
    "IsActive": true,
    "Created": "2017-02-22T05:15:36+00:00",
    "Modified": "2017-02-22T05:15:36+00:00"
},
{
    "SecurityQuestionID": 8,
    "Question": "What is your favorite movie?",
    "IsActive": true,
    "Created": "2017-02-22T05:15:10+00:00",
    "Modified": "2017-02-22T05:15:10+00:00"
},
{
    "SecurityQuestionID": 7,
    "Question": "Who is your favorite actor, musician, or artist?",
    "IsActive": true,
    "Created": "2017-02-22T05:14:55+00:00",
    "Modified": "2017-02-22T05:14:55+00:00"
},
{
    "SecurityQuestionID": 6,
    "Question": "What was your favorite place to visit as a child?",
    "IsActive": true,
    "Created": "2017-02-22T05:14:41+00:00",
    "Modified": "2017-02-22T05:14:41+00:00"
},
{
    "SecurityQuestionID": 5,
    "Question": "What time of the day were you born?",
    "IsActive": true,
    "Created": "2017-02-21T06:32:45+00:00",
    "Modified": "2017-02-21T06:33:00+00:00"
},
{
    "SecurityQuestionID": 4,
    "Question": "What is your first bike name?",
    "IsActive": true,
    "Created": "2017-02-21T06:22:14+00:00",
    "Modified": "2017-02-21T06:30:11+00:00"
},
{
    "SecurityQuestionID": 3,
    "Question": "What is your childhood best friend name ?",
    "IsActive": true,
    "Created": "2017-02-21T06:19:11+00:00",
    "Modified": "2017-02-21T06:21:43+00:00"
},
{
    "SecurityQuestionID": 1,
    "Question": "What is your pet name?",
    "IsActive": true,
    "Created": "2017-02-21T06:16:51+00:00",
    "Modified": "2017-02-21T06:21:20+00:00"
},
{
    "SecurityQuestionID": 2,
    "Question": "What is your first school teacher name?",
    "IsActive": true,
    "Created": null,
    "Modified": null
}

在我的控制器中,我设置了我的响应数据,如下所示

 $scope.question = result.data.responseData;

这是我的html块

      <div class="row" ng-repeat="n in [1, 2]">
                        <div class="col-sm-12">
                            <div class="form-group">
                                <label class="col-sm-4 control-label">
                                    Security Question {{n}}
                                </label>
                                <div class="col-sm-8">                                        
                                    <select class="form-control"                                                  
                                            name="SecurityQuestionID{{n}}"
                                            ng-disabled="action == 'View' || action == 'Edit'"
                                            ng-model="SecurityQuestion[$index].SecurityQuestionID"
                                            ng-selected="SecurityQuestion.SecurityQuestionID"
                                            ng-options="SecurityQuestion.SecurityQuestionID as SecurityQuestion.Question for SecurityQuestion in question"
                                            >    
                                    </select>
                                    <span class="validation-color"
                                          ng-show="myForm.SecurityQuestionID[$index].$touched && myForm.SecurityQuestionID[$index].$error.required">
                                        Security Question {{n}} is required<br>
                                    </span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-4 control-label">Answer {{n}}</label>
                                <div class="col-sm-8">
                                    <input type="hidden" ng-model="SecurityQuestion[$index].SecurityQuestionsAnswerID">
                                    <input type="text" 
                                           required="" 
                                           ng-model="SecurityQuestion[$index].Answer"
                                           ng-disabled="action === 'View'"                                              
                                           name="Answer{{n}}"
                                           class="form-control" 
                                           placeholder="Answer *">
                                    <span style="color:red" ng-show="myForm.Answer[$index].$dirty && myForm.Answer[$index].$invalid || myForm.Answer[$index].$touched">                                      
                                        <span ng-if="myForm.Answer[$index].$error.required">
                                            Answer {{n}} is required
                                        </span>
                                    </span>
                                </div>
                            </div>
                        </div>                      
                    </div>   

正如我在[1,2]中提到的那样。所以有两个下拉列表出现同样的问题。我希望如果我从第一个下拉列表中选择一个问题,那么它将在第二个下拉列表中被禁用。同样,如果我在第二个下拉列表中选择一个问题,它将在第一个下拉列表中被禁用。 任何人都可以帮助我做到这一点。

1 个答案:

答案 0 :(得分:0)

您必须在ng-options

中使用“禁用时间”选项

<强>控制器

$scope.questionSelected = {};

查看

<div class="row" ng-repeat="n in [1, 2]">
    <div class="col-sm-12">
        <div class="form-group">
            <label class="col-sm-4 control-label">
                Security Question {{n}}
            </label>
            <div class="col-sm-8">                                        
                <select class="form-control"                                                  
                        name="SecurityQuestionID{{n}}"
                        ng-disabled="action == 'View' || action == 'Edit'"
                        ng-model="questionSelected[$index]"
                        ng-selected="SecurityQuestion.SecurityQuestionID"
                        ng-options="SecurityQuestion.SecurityQuestionID as SecurityQuestion.Question disable when questionSelected[$index] == SecurityQuestion.SecurityQuestionID for SecurityQuestion in question"
                        >    
                </select>
                <span class="validation-color"
                      ng-show="myForm.SecurityQuestionID[$index].$touched && myForm.SecurityQuestionID[$index].$error.required">
                    Security Question {{n}} is required<br>
                </span>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-4 control-label">Answer {{n}}</label>
            <div class="col-sm-8">
                <input type="hidden" ng-model="SecurityQuestion[$index].SecurityQuestionsAnswerID">
                <input type="text" 
                       required="" 
                       ng-model="SecurityQuestion[$index].Answer"
                       ng-disabled="action === 'View'"                                              
                       name="Answer{{n}}"
                       class="form-control" 
                       placeholder="Answer *">
                <span style="color:red" ng-show="myForm.Answer[$index].$dirty && myForm.Answer[$index].$invalid || myForm.Answer[$index].$touched">                                      
                    <span ng-if="myForm.Answer[$index].$error.required">
                        Answer {{n}} is required
                    </span>
                </span>
            </div>
        </div>
    </div>                      
</div>