AngularJS选择标记验证
我在这里遗漏了一些非常简单的东西,我在我的控制器中得到了一个数组,如下所示AngularJS选择标记验证,angularjs,Angularjs,我在这里遗漏了一些非常简单的东西,我在我的控制器中得到了一个数组,如下所示 $scope.roles = [ {id: 0, label: 'Choose a Role'}, {id: 1, label: 'Administrator'}, {id: 2, label: 'Super User'} ]; 下面是我在视图中的选择标记 <select ng-options="role.id as role.label for r
$scope.roles = [
{id: 0, label: 'Choose a Role'},
{id: 1, label: 'Administrator'},
{id: 2, label: 'Super User'}
];
下面是我在视图中的选择标记
<select ng-options="role.id as role.label for role in roles" ng-model="myForm.role" ng-required="true">
</select>
我如何验证用户是否选择了
管理员
或超级用户
,而不是选择角色
?从您可以使用的HTML:
<div ng-if="myForm.role == 'Chose a role">
Your code when 'Chose a role;
</div>
从HTML中,您可以使用:
<div ng-if="myForm.role == 'Chose a role">
Your code when 'Chose a role;
</div>
Edit2:
编辑:我给你做了一把小提琴:
您可以通过以下方式添加选项:
<select ng-options="role.id as role.label for role in roles"
ng-model="myForm.role"
ng-required="true">
<option value="" disabled selected style="display: none;"> Chose a role </option>
</select>
选择角色
Edit2:
编辑:我给你做了一把小提琴:
您可以通过以下方式添加选项:
<select ng-options="role.id as role.label for role in roles"
ng-model="myForm.role"
ng-required="true">
<option value="" disabled selected style="display: none;"> Chose a role </option>
</select>
选择角色
试试这个简洁的解决方案。
使用此HTML可以创建select元素
HTML:
<select ng-model="myForm.role"
ng-options="role.id as role.label for role in roles"
ng-change="fillTheDDL(role.id, role.label)">
<option value="">Chose a role</option>
</select>
$scope.fillTheDDL= function (ID, Label) {
if (Label != "Choose a Role") {
// Your favorite code here
}
else {
// You don't wanna use it
}
};
试试这个简洁的解决方案。 使用此HTML可以创建select元素 HTML:
<select ng-model="myForm.role"
ng-options="role.id as role.label for role in roles"
ng-change="fillTheDDL(role.id, role.label)">
<option value="">Chose a role</option>
</select>
$scope.fillTheDDL= function (ID, Label) {
if (Label != "Choose a Role") {
// Your favorite code here
}
else {
// You don't wanna use it
}
};
但是在那种情况下,
myForm
将如何失效?但是在那种情况下,myForm
将如何失效?我在我的答案中添加了一个提琴,以便您可以有一个工作示例。请让我知道它是否适合你的需要。我在我的答案中添加了一个提琴,这样你就可以有一个有效的例子。请让我知道它是否适合您的需要。实际上它几乎就在那里了,只是所选角色的值必须是0
,而且我必须将表单设置为invalid@user3052526查看一下我的Edit2小提琴->如果没有选择任何值,则表单设置为无效(请参见消息),但我不明白您为什么要“选择角色”消息是否有值,因为选择此值时无法提交表单?这是后端api的要求。现在我要看一看实际上差不多了,除了选择角色的值必须是0
,而且我必须将表单设置为invalid@user3052526查看一下我的Edit2小提琴->如果没有选择任何值,则表单设置为无效(请参见消息),但我不明白您为什么要“选择角色”消息是否有值,因为选择此值时无法提交表单?这是后端api的要求。我现在要去看看