我的表单使用select来显示数据库中已选择的内容。所以我的localTypesObject是一个已经选中的项目。当用户选择另一个localTypesObject被更改时。
<select id="types" required multiple class="form-control" ng-model="localTypesObject" ng-options="item as item.getType() for item in types"></select>
现在我想我可以像以前一样使用ng-disabled禁用我的提交按钮:
<button type="submit" ng-disabled="form.$invalid" class="btn btn-default" ng-click="save()">Save</button>
但按钮永远不会启用,除非用户选择另一个项目或重新选择已选择的项目。然后表单设置为有效。
我该如何解决这个问题?在我看来,角度不会看出ng模型是否为空。
修改
这是在我的解析数据库中保存所有类型的函数。
$scope.applyTypes = function (result) {
$scope.types = result;
}
结果是Parse服务的结果。
当我有我的特定对象时,这是一个被调用的片段,然后循环查看必须选择的类型。
for(var indexAllTypes = 0; indexAllTypes < $scope.types.length; indexAllTypes++){
for(var indexMatchedTypes = 0; indexMatchedTypes < result.getTypes().length; indexMatchedTypes++){
if($scope.types[indexAllTypes].getType() == result.getTypes()[indexMatchedTypes].getType()){
$scope.localTypesObject.push($scope.types[indexAllTypes]);
}
}
}
这是我检查我的$ scope.types中的某个元素是否已经被选中的地方。
如您所见,我只是在scope.localTypesObject中推送相同的对象类型。
点击任何内容之前的输出:
点击某些内容后的输出: