?
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
月份輸入的驗(yàn)證和轉(zhuǎn)換。目前的瀏覽器還不支持HTML5的日期輸入控件,所以將使用文本輸入。 在這種情況下,輸入的文本必須符合ISO-8601的月份格式(yyyy-MM)的校驗(yàn)規(guī)范,例如:2009-01
。 模型必須永遠(yuǎn)是一個(gè)Date對(duì)象。In the event the model is not set to the first of the month, the first of that model's month is assumed.
<input Type="month"
ng-model=""
[name=""]
[min=""]
[max=""]
[required=""]
[ng-required=""]
[ng-change=""]>
參數(shù) | 類型 | 詳述 |
---|---|---|
ngModel | string | 聲明用于數(shù)據(jù)綁定的Angular表達(dá)式。 |
name (可選)
|
string | 發(fā)布到表單下的控件的屬性名稱。 |
min
(可選)
|
string | 當(dāng)輸入的值小于 |
max
(可選)
|
string | 當(dāng)輸入的值大于 |
required
(可選)
|
string | 如果未輸入值就設(shè)置 |
ngRequired
(可選)
|
string | 當(dāng)ngRequired表達(dá)式等于true時(shí),添加 |
ngChange
(可選)
|
string | Angular表達(dá)式,當(dāng)輸入元素通過(guò)用戶交互方式發(fā)生輸入變化時(shí)會(huì)執(zhí)行這個(gè)表達(dá)式。 |
<script>
angular.module('monthExample', [])
.controller('DateController', ['$scope', Function($scope) {
$scope.value = new Date(2013, 9, 1);
}]);
</script>
<form name="myForm" ng-controller="DateController as dateCtrl">
Pick a month int 2013:
<input id="exampleInput" Type="month" name="input" ng-model="value"
placeholder="yyyy-MM" min="2013-01" max="2013-12" required />
<span class="error" ng-show="myForm.input.$error.required">
Required!</span>
<span class="error" ng-show="myForm.input.$error.month">
Not a valid month!</span>
<tt>value = {{value | date: "yyyy-MM"}}</tt><br/>
<tt>myForm.input.$valid = {{myForm.input.$valid}}</tt><br/>
<tt>myForm.input.$error = {{myForm.input.$error}}</tt><br/>
<tt>myForm.$valid = {{myForm.$valid}}</tt><br/>
<tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br/>
</form>
protractor.js
var value = element(by.binding('value | date: "yyyy-MM"'));
var valid = element(by.binding('myForm.input.$valid'));
var input = element(by.model('value'));
// currently protractor/webdriver does not support
// sending keys to all known HTML5 input controls
// for various browsers (https://github.com/angular/protractor/issues/562).
Function setInput(val) {
// set the value of the element and force validation.
var scr = "var ipt = document.getElementById('exampleInput'); " +
"ipt.value = '" + val + "';" +
"angular.element(ipt).scope().$apply(function(s) { s.myForm[ipt.name].$setViewValue('" + val + "'); });";
browser.executeScript(scr);}
it('should initialize to model', Function() {
expect(value.getText()).toContain('2013-10');
expect(valid.getText()).toContain('myForm.input.$valid = true');});
it('should be invalid if empty', Function() {
setInput('');
expect(value.getText()).toEqual('value =');
expect(valid.getText()).toContain('myForm.input.$valid = false');});
it('should be invalid if over max', Function() {
setInput('2015-01');
expect(value.getText()).toContain('');
expect(valid.getText()).toContain('myForm.input.$valid = false');});