如何使用Angular 2.0中的formControl访问Native HTML Input元素

AIo*_*Ion 6 onfocus rxjs angular2-forms angular2-formbuilder angular

我正在使用Angular 2.0最终版本.

我想做的事? - 我想只在输入接收焦点时显示一些通知(警告,输入要求).甚至更好,当他的父母(the div)有mouse hover事件.

从父(div)这样做很容易.Just(focus)= showNotifications()+ an ngIf - 完成工作.

但我想在show-notifications组件中封装此功能 - 并使其可重用..

鉴于我通过the control使用显示通知内部@Input()- 我可以做这两件事,如果我有权访问native HTML input element.你可以看到如何进入show-notifications.component.ts.这是代码:

app.component.html:

`<div>
   <label>Name: </label>
   <input formControlName="myName">
   <show-notifications [the_control]="myName" ></show-notifications>
</div>`
Run Code Online (Sandbox Code Playgroud)

app.component.ts:

export class AppComponent {
    myName = new FormControl("defaultvalue",[some validators..])
}
Run Code Online (Sandbox Code Playgroud)

显示-notifications.component.html:

<div class="show_requirements" *ngIf="hasfocus or parentDivHasMouseHover"> // incorect code and logic - i know, but you can see the idea..

    <p *ngFor="let requirement of thisInputRequirements">{{requirement}}<p>

</div>
Run Code Online (Sandbox Code Playgroud)

显示-notifications.component.ts:

export class ShowNotificationsComponent {

    @Input() the_control;
    this.thisInputRequirements = // take them form firebase.
    this.thisInputCustomErrorMessages = // take them from firebase.

    // I implemented all this and works amazing but i want to do:

    //something like this:

    ngOnInit(){
        this.nativeInputElement = this.the_control.getNativeElement() // this of course doesn't work

        // the requirements are shown only when the input receive focus
        let source = Rx.DOM.focus(this.nativeInputElement);
        source.subscribe( x => this.hasFocus = true)

        // Or even better: 
        // the requirements are shown only when the parent has mouse hover
        // or any other event / endles posibilites here..

        let parent = getParentOf(this.nativeInputElement)
        let source = Rx.DOM.mouseover(parent);
        source.subscribe( x => this.parentDivHasMouseHover = true) // this will be some toggling functionality.
    }

}
Run Code Online (Sandbox Code Playgroud)

题:

如果我有formControl(myName = the_control)对象,我如何访问本机元素?

有没有更好的方法在单独的组件中进行通知 - 并使其可重用?我已经在整个应用程序中成功使用它 - 显示错误和输入要求..

注意:我尝试首先使用hashtag语法(#myInput)传递hole html输入并在那里形成,在show-notifications组件中,我试图这样做:myInput.myName - 访问控件但我得到了未定义.控件不存在于本机输入元素上.我需要那个控制来验证:)

mar*_*tin 4

我认为您可以使用#符号将元素传递给ShowNotificationsComponent

export class ShowNotificationsComponent {
    @Input() the_control;
    @Input() elm;
    //...
}
Run Code Online (Sandbox Code Playgroud)

然后在模板中:

<input formControlName="myName" #elm>
<show-notifications [the_control]="myName" [elm]="elm"></show-notifications>
Run Code Online (Sandbox Code Playgroud)

  • 是的@Martin,感谢您的回答,我已经这样做了,并且工作正常,但我仍然在等待一个允许我访问控件内的本机元素表单的解决方案。我不敢相信他们没有以某种方式联系在一起。形成本机输入元素我无法获得控制。从控件中我无法获取本机元素。怎么会这样?听起来不对。我需要在我的代码中创建所有这些 # 变量。不干净。但是,是的,现在有效:) (3认同)
  • 好吧,如果您查看“FormControl”和“AbstractControl”的源代码,它实际上不会在任何地方访问本机元素。请参阅 https://github.com/angular/angular/blob/2.1.0-beta.0/modules/%40angular/forms/src/model.ts#L76-L608 和 https://github.com/angular/角度/blob/2.1.0-beta.0/modules/%40angular/forms/src/model.ts#L608-L798 (2认同)