Angular 属性指令表单输入
Angular Attribute Directive form input
我正在努力让 @Directive 按预期工作。
我希望 Attr 指令能够访问指令构造函数中的 model.password 并使用当前密码设置变量 varpassword。
我还希望能够检索表单字段中的所有其他输入,我该怎么做?
据我所知,自从处于 Beta 阶段以来,其中很多内容的文档都不完整。
几天来我一直在努力解决这个问题,但收效甚微。我已经删除了我试图让它工作的导入,因为我想我很困惑如何在我尝试过的指令中正确使用它们 "NgModel, NgControl, ControlGroup, NgFormModel, FormBuilder...".
下面是我的测试示例代码。
登录-form.html
<form #testForm="ngForm">
Email Address:
<input type="text" name="username" size="50" required="required" [(ngModel)]="model.username" ngControl="username" #username="ngForm" />
Password:
<input type="password" name="password" size="20" required="required" [(ngModel)]="model.password" ngControl="password" #password="ngForm" testdir />
<input type="submit" value="Login" />
</form>
app.ts
import {Component} from 'angular2/core';
import {TestDirective} from '../../directives/test.directive';
@Component({
selector: 'test-app',
templateUrl: 'login-form.html',
directives: [TestDirective]
})
export class TestApp {}
test.directive.ts
import {Directive} from 'angular2/core';
@Directive({
selector: '[testdir]',
host: {
'(keydown)': 'run()'
}
})
export class TestDirective {
varpassword: string;
constructor() {
this.varpassword = [******]
}
run() {
console.log( this.varpassword );
}
}
如果有人能指出正确的方向,我将不胜感激。
您需要将 NgControl
注入您的指令:
@Directive({
(...)
})
export class TestDirective {
varpassword: string;
constructor(private ctrl:NgControl) {
}
ngOnInit() {
this.varpassword = this.ctrl.value;
}
}
Se this plunker: https://plnkr.co/edit/3y4Qf7M4hb3zDIEJ773Q?p=preview.
焦点移开时带逗号的输入指令示例。
import {Directive, ElementRef} from '@angular/core';
@Directive({
selector: '[numberInput]',
host: {
'(focus)': '_onFocus()',
'(blur)': '_onBlur()',
'(keydown)': '_onKeypress($event)'
}
})
export class InputNumberDirective {
inputElement: ElementRef;
constructor(el: ElementRef) {
this.inputElement = el;
}
_onBlur() {
this.inputElement.nativeElement.value = this.inputElement.nativeElement.value.length > 0 ? parseInt(this.inputElement.nativeElement.value).toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, ",") : '';
}
_onKeypress(event: KeyboardEvent) {
if((event.which>= 58 && event.which<=126) || (event.keyCode >= 33 && event.which<= 47)){
event.preventDefault();
}
return event;
}
}
我正在努力让 @Directive 按预期工作。
我希望 Attr 指令能够访问指令构造函数中的 model.password 并使用当前密码设置变量 varpassword。
我还希望能够检索表单字段中的所有其他输入,我该怎么做?
据我所知,自从处于 Beta 阶段以来,其中很多内容的文档都不完整。
几天来我一直在努力解决这个问题,但收效甚微。我已经删除了我试图让它工作的导入,因为我想我很困惑如何在我尝试过的指令中正确使用它们 "NgModel, NgControl, ControlGroup, NgFormModel, FormBuilder...".
下面是我的测试示例代码。
登录-form.html
<form #testForm="ngForm">
Email Address:
<input type="text" name="username" size="50" required="required" [(ngModel)]="model.username" ngControl="username" #username="ngForm" />
Password:
<input type="password" name="password" size="20" required="required" [(ngModel)]="model.password" ngControl="password" #password="ngForm" testdir />
<input type="submit" value="Login" />
</form>
app.ts
import {Component} from 'angular2/core';
import {TestDirective} from '../../directives/test.directive';
@Component({
selector: 'test-app',
templateUrl: 'login-form.html',
directives: [TestDirective]
})
export class TestApp {}
test.directive.ts
import {Directive} from 'angular2/core';
@Directive({
selector: '[testdir]',
host: {
'(keydown)': 'run()'
}
})
export class TestDirective {
varpassword: string;
constructor() {
this.varpassword = [******]
}
run() {
console.log( this.varpassword );
}
}
如果有人能指出正确的方向,我将不胜感激。
您需要将 NgControl
注入您的指令:
@Directive({
(...)
})
export class TestDirective {
varpassword: string;
constructor(private ctrl:NgControl) {
}
ngOnInit() {
this.varpassword = this.ctrl.value;
}
}
Se this plunker: https://plnkr.co/edit/3y4Qf7M4hb3zDIEJ773Q?p=preview.
焦点移开时带逗号的输入指令示例。
import {Directive, ElementRef} from '@angular/core';
@Directive({
selector: '[numberInput]',
host: {
'(focus)': '_onFocus()',
'(blur)': '_onBlur()',
'(keydown)': '_onKeypress($event)'
}
})
export class InputNumberDirective {
inputElement: ElementRef;
constructor(el: ElementRef) {
this.inputElement = el;
}
_onBlur() {
this.inputElement.nativeElement.value = this.inputElement.nativeElement.value.length > 0 ? parseInt(this.inputElement.nativeElement.value).toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, ",") : '';
}
_onKeypress(event: KeyboardEvent) {
if((event.which>= 58 && event.which<=126) || (event.keyCode >= 33 && event.which<= 47)){
event.preventDefault();
}
return event;
}
}