我正在使用 Angular 2.0 最终版本。
我想做的事?
- 我只想在输入接收焦点时显示一些通知(警告,输入要求)。或者更好的是,当他的父母 ( the div) 有mouse hover事件时。
从父级(div)执行此操作很容易。只需 (focus)=showNotifications() + 一个 ngIf - 就完成了。
但我想将此功能封装在 show-notifications 组件中 - 并使其可重用..
鉴于我the control使用 - 在显示通知内部传递@Input()- 如果我可以访问native HTML input element.You 可以看到如何在show-notifications.component.ts. 这是代码:
app.component.html:
`<div>
<label>Name: </label>
<input formControlName="myName">
<show-notifications [the_control]="myName" ></show-notifications>
</div>`
app.component.ts:
export class AppComponent {
myName = new FormControl("defaultvalue",[some validators..])
}
显示通知.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>
显示通知.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.
}
}
问题:
鉴于我有 formControl (myName = the_control) 对象,我如何访问本机元素?
有没有更好的方法在单独的组件中进行通知 - 并使其可重用?我已经在整个应用程序中成功使用了它 - 显示错误和输入要求..
注意:我尝试首先使用主题标签语法(#myInput)传递洞 html 输入并在那里形成,在 show-notifications 组件内,我尝试执行:myInput.myName - 访问控件但我未定义。本机输入元素上不存在这些控件。我需要该控件进行验证:)