我创建了一个嵌套类别模型。为了选择产品的类别,您会看到一个包含主要类别的下拉菜单。当您选择一个时,将在右侧添加一个新的下拉列表,其中包含您选择的子类别的子类别。重复此过程,直到您到达最后一个级别。发生这种情况时$scope.product.category_id
设置。我想在$scope.product.category_id
is时使整个字段集无效null
。
我一直在使用angular-bootstrap-show-errors进行验证,我尝试将它与ui-utils混合使用自定义函数来实现这一点:validate_category()
.
这是我使用的 HTML:
<span ng-repeat="parent_id in category_dropdown_parents" show-errors="{ skipFormGroupCheck: true }">
<select class="form-control" name="category_id"
ng-init="selected_category[$index] = init_select($index);"
ng-model="selected_category[$index]"
ng-options="category.name for category in (categories | filter : { parent_id: parent_id } : true ) track by category.id "
ng-change="select_category(selected_category[$index], $index)"
ui-validate="'validate_category()'" // added this to work with ui-utils
>
</select>
<span ng-if="$index+1 != category_dropdown_parents.length">/</span>
</span>
这是我的简单验证功能:
$scope.validate_category = function() {
return ( $scope.product.category_id !== null
&& $scope.product.category_id !== undefined);
}
但这不起作用。想法?
编辑:我刚刚意识到,这个问题是验证函数在函数ui-validate
之后执行ng-change
,所以它永远无法检查$scope.product.category_id
更新。