首页 > 解决方案 > 验证 ng-repeat 表中的选择选项

问题描述

如果所有选择选项都为空,我想验证或检查 ng-repeat 表中的选择数组选项。然后,假设 2 选择选项是否具有值如何获取这些值。在此先感谢,非常感谢您的帮助。

html

<button type="button" ng-click="checkSelect()">Check</button>
<button type="button" ng-click="getvalue()">get</button>
<table><tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td>
    <select ng-model="person.numPerson[$index]" ng-options="num for num in [] | range:5">
        <option value="">0</option>
    </select>
</td></tr></table>

js

var app = angular.module('app', []);
app.controller('Controller', function($scope, $http){

        //for validation
        $scope.checkSelect = function () {
        }

        //get all selected select value
        $scope.getvalue = function () {
        }


    });

标签: angularjs

解决方案


请使用以下更新您的 checkSelect 功能

//for validation
$scope.checkSelect = function () {
    angular.forEach($scope.person.numPerson, function(value, key) {
        if(value == ''){
            //Show alert or maintain a flag to show error to users
            //perform actions 
        }
    }); 
}

推荐阅读