http://plnkr.co/edit/9Yq6aZHtKCuZMtPbjBIN?p=preview
我已经尝试过尝试让它发挥作用,但我没有运气。我尝试使用angularJS和内置验证功能禁用提交按钮。我发现当您第一次加载表单时,提交按钮处于活动状态 - 未禁用!
我已经过测试和尝试,但我发现我的验证码在团队名称中接受了空字符串/空字符串,尽管我需要输入。
有谁知道如何正确格式化?我实现它的唯一方法就是用一个空格替换一个空字符串来捏造数据......在生产中这是不可接受的......
这是index.html:
<body ng-controller="EnterController as enter">
<div class="panel panel-primary">
<div class="panel-body">
<form name="enterFormNew" ng-submit="enter.TeamNameNext()" autocomplete="off" novalidate>
<div class="row">
<div class="col-xs-8 col-md-6">
<div class="form-group">
<label for="teamname">Team Name</label>
<input name="TeamName" ng-required ng-minlength="2" ng-maxlength="40" ng-model="enter.Team.Name" type="text" id="teamname" class="form-control" />
<p ng-show="enterFormNew.TeamName.$touched && enterFormNew.TeamName.$invalid">This is not a valid team name.</p>
</div>
<div class="form-group">
<label for="division">Division</label>
<select name="selectDivision" ng-required ng-model="enter.Team.Division" id="division" class="form-control" ng-options="division.Name for division in enter.Divisions track by division.Id ">
<option value="">Select...</option>
</select>
<p ng-show="enterFormNew.selectDivision.$touched && enterFormNew.selectDivision.$invalid">A valid Division needs to be selected.</p>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-2">
<button type="submit" class="btn btn-primary" ng-disabled="enterFormNew.$invalid">Next</button>
</div>
</div>
</form>
</div>
</div>
</body>
这是app.js:
angular.module('Enter', [])
.controller("EnterController", [
function() {
this.RegistrationPhase = 0;
this.Divisions = [{ Id: 1,Name: "Normal"}, {Id: 2,Name: "Not Normal"}];
this.Team = { Name: "", ResumeKey: null, Division: { Id: -1 }, Players: []};
this.TeamNameNext = function() {
//Code removed
alert("Made it to the submit function!")
};
}
]);
答案 0 :(得分:0)
属性ng-required
需要一个值。不要与不需要值的HTML5属性required
混淆。
ng-required="true"
答案 1 :(得分:0)
因为&#34; Team.Division&#34;在控制器代码中未定义。
如果你可以使用下面的代码,你将得到你所展开的内容。
this.Team = { Name: "", ResumeKey: null, Division: undefined, Players: []};
您可以使用null或&#34;&#34;而不是未定义。