如何使用angularjs验证下拉列表?

时间:2017-12-06 22:09:35

标签: javascript html angularjs

我有一个文本框和下拉栏。如果用户在文本框中输入任何内容,我们需要让用户从下拉列表中选择任何人(这意味着需要用户)。如果用户从文本框中删除了输入的值,则用户无需选择下拉列表(这意味着文本框可能为空,因此无需选择下拉列表)帮助我向前推进。 我在下面添加了我的代码



angular
  .module('MyApp',['ngMaterial', 'ngMessages', 'material.svgAssetsCache'])
    .controller('DemoCtrl', function ($scope) {
        $scope.users = ['','abs','xcv'];
    
      
   
    })
       
   
  .config(function($mdThemingProvider) {

    

    $mdThemingProvider.theme('docs-dark', 'default')
      .primaryPalette('green')
      .dark();

  });

 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic">
    <link rel='stylesheet prefetch' href='https://cdn.gitcdn.link/cdn/angular/bower-material/v1.1.5/angular-material.css'>
    <link rel='stylesheet prefetch' href='https://material.angularjs.org/1.1.5/docs.css'>
    <div ng-controller="DemoCtrl" layout="column" ng-cloak="" class="md-inline-form inputdemoBasicUsage" ng-app="MyApp">

        <md-card class="asd">
  <div >
       <form name="form">
        <div layout-gt-sm="row">
          <md-input-container class="md-block" layout-xs>
            <label>User</label>
            <md-select ng-model="" >
              <md-option ng-repeat="user in users" value="{{user}}">{{user}}</md-option>
            </md-select>
          </md-input-container>
          <md-input-container class="md-block" flex-xs>
            <label>Name</label>
            <input ng-model="name" >
          </md-input-container>
          
         
        </div>
        
       </form>

  </div>
    
  </md-card>
           

    </div>
     <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.js'></script>
    <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.min.js'></script>
    <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-route.min.js'></script>
    <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-aria.min.js'></script>
    <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-messages.min.js'></script>
    <script src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-114/svg-assets-cache.js'></script>
    <script src='https://cdn.gitcdn.link/cdn/angular/bower-material/v1.1.5/angular-material.js'></script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可以添加ng-if以选择

<md-select ng-model="" ng-if="textBoxModel == ''">
         <md-option ng-repeat="user in users" value="{{user}}">{{user}}</md-option>
</md-select>

记住“textBoxModel”是分配给文本输入的模型