【问题标题】:Form with dynamic select fields does not validate具有动态选择字段的表单无法验证
【发布时间】:2013-09-13 16:20:47
【问题描述】:

这里我基本上设置了 3 个选择列表

<label for="organization">Organization</label>
<select ng-change="setOrganization()" ng-model="form.organization" 
        ng-options="o.name as o.displayName for o in organizations" required></select>

<label for="board">Board</label>
<select ng-change="setBoard()" ng-model="form.board" 
        ng-options="b.id as b.name for b in boards" required></select>

<label for="board">List</label>
<select ng-change="setList()" ng-model="form.list" 
        ng-options="l.id as l.name for l in lists" required></select>

JS:

$scope.setOrganization = function(){
    $scope.lists = {};

    mySerivce.get('....', function(boards){
        $scope.boards = angular.fromJson(boards);
        $scope.$apply();
    });
};

$scope.setBoard = function(){
    mySerivce.get('...', function(lists){
        $scope.lists = angular.fromJson(lists);
        $scope.$apply();
    });
};

因此,当一个组织被选中时,我会得到板子并用板子数据填充第二个选择字段。再次选择电路板时,将获取分配给电路板的所有列表,并使用此数据设置第三种选择字段。在所有选择字段都具有选定值之前,表单将保持无效。

当用户更改组织时,所有字段都会被清除。但是 form.$valid 对象仍然是 true。哪里有问题?当没有选择任何值时,表单必须是无效的,因为这些字段是必需的。有什么想法吗?

【问题讨论】:

  • 您可以在 get 回调中设置 $scope.form.board = null 吗?
  • @ZackArgyle 效果很好!让这成为一个答案

标签: javascript angularjs angularjs-scope


【解决方案1】:

试试这个,它会验证选择值没有设置。很高兴它成功了。

mySerivce.get('....', function(boards){
    $scope.boards = angular.fromJson(boards);
    $scope.form.board = null;
    $scope.$apply();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 2018-09-08
    • 2015-07-17
    相关资源
    最近更新 更多