验证所需的ui-select字段

时间:2018-06-11 17:29:03

标签: angularjs twitter-bootstrap ui-select

我是角色新手,我需要一种验证ui-select字段的方法,我尝试过

  

NG-需要="真"

  

需要选用

但由于现在没有这些工作我想使用bootstrap has-error类来显示该字段是必需的,这是我的代码。

<div class="form-group">
<div ng-class="{'has-error': !project.parentOrganization || project.parentOrganization == ''}">
<ui-select ng-model="project.parentOrganization" theme="bootstrap" reset-search-input="true" tabindex="4" ng-required="true" id="{{'itopOrganizationId' + $index}}" required>
  <ui-select-match placeholder="Seleccione una organización padre">{{$select.selected.name}}</ui-select-match>
  <ui-select-choices repeat="value.id as value in parentOrganizations | filter: {name: $select.search} | orderBy:'name'">
    <div ng-bind-html="value.name | highlight: $select.search"></div>
  </ui-select-choices>
</ui-select>

这是我的控制器的一部分

    $scope.saveProject = function () {

  if(!$scope.project.parentOrganization || $scope.project.parentOrganization == ""){
    return
 } 

现在的问题是,当模态显示已标记为红色时,我需要在单击提交按钮后执行此操作。

我该怎么办?你有更好的方法来实现这个吗?

1 个答案:

答案 0 :(得分:0)

制作一个这样的表格

<form ng-form="example" novalidate>
<div class="form-group">
<div ng-class="{'has-error': example.project.parentOrganization.$invalid && !example.project.parentOrganization.$pristine}">
<ui-select name="project.parentOrganization" ng-model="project.parentOrganization" theme="bootstrap" reset-search-input="true" tabindex="4" ng-required="true" id="{{'itopOrganizationId' + $index}}" required>
  <ui-select-match placeholder="Seleccione una organización padre">{{$select.selected.name}}</ui-select-match>
  <ui-select-choices repeat="value.id as value in parentOrganizations | filter: {name: $select.search} | orderBy:'name'">
    <div ng-bind-html="value.name | highlight: $select.search"></div>
  </ui-select-choices>
</ui-select>
</form>

并在您的控制器中

$scope.example={}
$scope.saveProject = function () {

  if(!$scope.project.parentOrganization || $scope.project.parentOrganization == ""){
      $scope.example.project.parentOrganization.$pristine=false; 
      return
    }