Angular 表单验证但没有 <form>?
Angular form validation but without <form>?
我正在尝试使用 angular material 框架获得一个非常简单的 angular 验证表单。我正在 Sharepoint 2010 内容编辑器 Web 部件中构建此表单,该表单允许使用 .我正在努力重建表格而不是在网页上,但没有涉及其他更复杂的问题。
但我深入探讨了其他问题,我只是想知道 angular 验证表单是否可以在没有该元素的情况下工作?或者是否有其他指令可以替代它?
这是我的目标但没有表单元素的一个小例子。
http://jsfiddle.net/jhadesdev/yfLqfzLw/2/?utm_source=website&utm_medium=embed&utm_campaign=yfLqfzLw
<div class="container" id="demoApp">
<form class="pure-form pure-form-aligned" name="frm" method="post"
novalidate autocomplete="off">
<fieldset>
<div class="pure-control-group">
<label>Username</label>
<input name="username" ng-model="user.username" type="text" placeholder="Username" required>
<div class="field-message" ng-messages="frm.username.$error" ng-if='frm.username.$dirty' ng-cloak>
<div ng-message="required">Username is required</div>
</div>
</div>
<div class="pure-control-group">
<label>Password</label>
<input name="password"
ng-model="user.password"
type="password"
placeholder="Password"
required
ng-minlength="6"
ng-maxlength="10">
<div class="field-message" ng-messages="frm.password.$error" ng-if='frm.password.$dirty' ng-cloak>
<div ng-message="required">Password is required</div>
<div ng-message="minlength">Password must have minimum 6 characters</div>
<div ng-message="maxlength">Password must have maximum 10 characters</div>
</div>
</div>
<div class="pure-control-group">
<label>Email Address</label>
<input name="email"
ng-model="user.email"
type="email"
placeholder="Email Address"
required>
<div class="field-message" ng-messages="frm.email.$error" ng-if='frm.email.$dirty' ng-cloak>
<div ng-message="required">Email is required</div>
<div ng-message="email">Must be a valid email</div>
</div>
</div>
<div class="pure-controls">
<label class="pure-checkbox">
<input name="conditions" ng-model="conditions" type="checkbox">
I've read the terms and conditions
</label>
<button type="submit" class="pure-button pure-button-primary"
ng-disabled="frm.$invalid || !conditions">Submit</button>
</div>
</fieldset>
</form>
</div>
有人知道这方面的信息吗?没能找到这方面的文档。
AngularJs ngForm direcive 不要求元素必须是 <form>
,如果您愿意,可以在 div
中使用 ng-form
。例如,取自您的 fiddle,您用这个 div
替换了之前的 form
元素。
<div ng-form="frm" class="pure-form pure-form-aligned">
...
</div>
完整的工作片段:
angular.module('DemoApp', ['ngMessages']);
angular.bootstrap(document.getElementById('demoApp'), ['DemoApp']);
@import "http://yui.yahooapis.com/pure/0.6.0/pure-min.css";
.container {
width: 970px;
padding-top: 10px;
margin: 0 auto;
}
.pure-form input.ng-dirty.ng-invalid {
border-color: #e9322d;
}
.field-message {
display: inline-block;
color: #e9322d;
font-size: 90%;
margin-left: 0.5em;
}
[ng\:cloak],
[ng-cloak],
[data-ng-cloak],
[x-ng-cloak],
.ng-cloak,
.x-ng-cloak {
display: none !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-messages.js"></script>
<div class="container" id="demoApp">
<div ng-form="frm" class="pure-form pure-form-aligned" autocomplete="off">
<fieldset>
<div class="pure-control-group">
<label>Username</label>
<input name="username" ng-model="user.username" type="text" placeholder="Username" required>
<div class="field-message" ng-messages="frm.username.$error" ng-if='frm.username.$dirty' ng-cloak>
<div ng-message="required">Username is required</div>
</div>
</div>
<div class="pure-control-group">
<label>Password</label>
<input name="password" ng-model="user.password" type="password" placeholder="Password" required ng-minlength="6" ng-maxlength="10">
<div class="field-message" ng-messages="frm.password.$error" ng-if='frm.password.$dirty' ng-cloak>
<div ng-message="required">Password is required</div>
<div ng-message="minlength">Password must have minimum 6 characters</div>
<div ng-message="maxlength">Password must have maximum 10 characters</div>
</div>
</div>
<div class="pure-control-group">
<label>Email Address</label>
<input name="email" ng-model="user.email" type="email" placeholder="Email Address" required>
<div class="field-message" ng-messages="frm.email.$error" ng-if='frm.email.$dirty' ng-cloak>
<div ng-message="required">Email is required</div>
<div ng-message="email">Must be a valid email</div>
</div>
</div>
<div class="pure-controls">
<label class="pure-checkbox">
<input name="conditions" ng-model="conditions" type="checkbox">I've read the terms and conditions
</label>
<button type="submit" class="pure-button pure-button-primary" ng-disabled="frm.$invalid || !conditions">Submit</button>
</div>
</fieldset>
</div>
</div>
我正在尝试使用 angular material 框架获得一个非常简单的 angular 验证表单。我正在 Sharepoint 2010 内容编辑器 Web 部件中构建此表单,该表单允许使用 .我正在努力重建表格而不是在网页上,但没有涉及其他更复杂的问题。
但我深入探讨了其他问题,我只是想知道 angular 验证表单是否可以在没有该元素的情况下工作?或者是否有其他指令可以替代它?
这是我的目标但没有表单元素的一个小例子。
http://jsfiddle.net/jhadesdev/yfLqfzLw/2/?utm_source=website&utm_medium=embed&utm_campaign=yfLqfzLw
<div class="container" id="demoApp">
<form class="pure-form pure-form-aligned" name="frm" method="post"
novalidate autocomplete="off">
<fieldset>
<div class="pure-control-group">
<label>Username</label>
<input name="username" ng-model="user.username" type="text" placeholder="Username" required>
<div class="field-message" ng-messages="frm.username.$error" ng-if='frm.username.$dirty' ng-cloak>
<div ng-message="required">Username is required</div>
</div>
</div>
<div class="pure-control-group">
<label>Password</label>
<input name="password"
ng-model="user.password"
type="password"
placeholder="Password"
required
ng-minlength="6"
ng-maxlength="10">
<div class="field-message" ng-messages="frm.password.$error" ng-if='frm.password.$dirty' ng-cloak>
<div ng-message="required">Password is required</div>
<div ng-message="minlength">Password must have minimum 6 characters</div>
<div ng-message="maxlength">Password must have maximum 10 characters</div>
</div>
</div>
<div class="pure-control-group">
<label>Email Address</label>
<input name="email"
ng-model="user.email"
type="email"
placeholder="Email Address"
required>
<div class="field-message" ng-messages="frm.email.$error" ng-if='frm.email.$dirty' ng-cloak>
<div ng-message="required">Email is required</div>
<div ng-message="email">Must be a valid email</div>
</div>
</div>
<div class="pure-controls">
<label class="pure-checkbox">
<input name="conditions" ng-model="conditions" type="checkbox">
I've read the terms and conditions
</label>
<button type="submit" class="pure-button pure-button-primary"
ng-disabled="frm.$invalid || !conditions">Submit</button>
</div>
</fieldset>
</form>
</div>
有人知道这方面的信息吗?没能找到这方面的文档。
AngularJs ngForm direcive 不要求元素必须是 <form>
,如果您愿意,可以在 div
中使用 ng-form
。例如,取自您的 fiddle,您用这个 div
替换了之前的 form
元素。
<div ng-form="frm" class="pure-form pure-form-aligned">
...
</div>
完整的工作片段:
angular.module('DemoApp', ['ngMessages']);
angular.bootstrap(document.getElementById('demoApp'), ['DemoApp']);
@import "http://yui.yahooapis.com/pure/0.6.0/pure-min.css";
.container {
width: 970px;
padding-top: 10px;
margin: 0 auto;
}
.pure-form input.ng-dirty.ng-invalid {
border-color: #e9322d;
}
.field-message {
display: inline-block;
color: #e9322d;
font-size: 90%;
margin-left: 0.5em;
}
[ng\:cloak],
[ng-cloak],
[data-ng-cloak],
[x-ng-cloak],
.ng-cloak,
.x-ng-cloak {
display: none !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-messages.js"></script>
<div class="container" id="demoApp">
<div ng-form="frm" class="pure-form pure-form-aligned" autocomplete="off">
<fieldset>
<div class="pure-control-group">
<label>Username</label>
<input name="username" ng-model="user.username" type="text" placeholder="Username" required>
<div class="field-message" ng-messages="frm.username.$error" ng-if='frm.username.$dirty' ng-cloak>
<div ng-message="required">Username is required</div>
</div>
</div>
<div class="pure-control-group">
<label>Password</label>
<input name="password" ng-model="user.password" type="password" placeholder="Password" required ng-minlength="6" ng-maxlength="10">
<div class="field-message" ng-messages="frm.password.$error" ng-if='frm.password.$dirty' ng-cloak>
<div ng-message="required">Password is required</div>
<div ng-message="minlength">Password must have minimum 6 characters</div>
<div ng-message="maxlength">Password must have maximum 10 characters</div>
</div>
</div>
<div class="pure-control-group">
<label>Email Address</label>
<input name="email" ng-model="user.email" type="email" placeholder="Email Address" required>
<div class="field-message" ng-messages="frm.email.$error" ng-if='frm.email.$dirty' ng-cloak>
<div ng-message="required">Email is required</div>
<div ng-message="email">Must be a valid email</div>
</div>
</div>
<div class="pure-controls">
<label class="pure-checkbox">
<input name="conditions" ng-model="conditions" type="checkbox">I've read the terms and conditions
</label>
<button type="submit" class="pure-button pure-button-primary" ng-disabled="frm.$invalid || !conditions">Submit</button>
</div>
</fieldset>
</div>
</div>