简体   繁体   English

Angular必需的选择选项不适用于占位符

[英]Angular required select option not working for placeholder

plunkr here 在这里

I have a default select value properly not fulfilling the required validation: 我有一个默认选择值,不能正确执行所需的验证:

<form name="testForm" novalidate>
  <select ng-model="model.selecter" required ng-options="opt for opt in options">
    <option value="">Select One</option>
  </select>
</form>

$scope.options = [
    'First',
    'Second',
    'Third'
];

While "Select One" is selected, testForm.$valid is false , as it should be. 当选择“选择一个”时, testForm.$validfalse ,这是应该的。

However, I would like to be able to add my placeholder to the options array as well, but in this instance it marks it as valid. 但是,我也希望能够将我的占位符添加到options数组中,但是在这种情况下,它将其标记为有效。

<form name="testForm" novalidate>
  <select ng-model="model.selecter" required ng-options="opt for opt in options">
  </select>
</form>

$scope.options = [
    'Select One',
    'First',
    'Second',
    'Third'
];

$scope.model = { selecter: $scope.options[0] }; // sets the default selected option

Here testForm.$valid is always true . 在这里, testForm.$valid始终为true Is there any way to make my Select One option not fulfill the required status of the form? 有什么方法可以使我的“ Select One选项不满足表单的required状态?

Not sure why you are trying to complicate something that works, but if you must you will have to make some other changes as well. 不知道为什么要尝试使有用的东西复杂化,但是如果必须的话,还必须进行一些其他更改。 Change your select to be like this 更改您的选择像这样

             <select ng-model="model2.selecter" required ng-options="opt.value as opt.label for opt in options2">
             </select>

And your values to be like this 和你这样的价值观

      $scope.options2 = [
        {label:'Select One',value:''},
        {label:'First',value:'1'}
      ];

And do not give your model variable a value. 并且不要给您的模型变量一个值。 Make it blank. 将其空白。

         $scope.model2 = { 
            selecter: ''
        };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM