我是离子的新手。我正在使用 Ionic 框架 3。我的问题是我不知道如何访问 ion-input 指令包围的输入值。我想访问我创建的自定义指令的输入框的值。
ElementRef对获取输入框的值有帮助吗?我尝试过但失败了。请指导我访问自定义指令中输入框的值的正确方法。下面是我的代码...
我的自定义指令代码-电话号码
import { Directive, HostListener, ElementRef } from '@angular/core';
/**
* Generated class for the PhonenumberDirective directive.
*
* See https://angular.io/api/core/Directive for more info on Angular
* Directives.
*/
@Directive({
selector: '[phonenumber]' // Attribute selector
})
export class PhonenumberDirective {
constructor(private element: ElementRef) {
console.log('Hello PhonenumberDirective Directive');
}
@HostListener('keydown', ['$event']) onkeydown(event) {
let inputValue = this.element.nativeElement.textContent;
// Here inputValue is undefined I am getting :-(
}
}
Run Code Online (Sandbox Code Playgroud)
HTML 代码
<ion-list inset> …
Run Code Online (Sandbox Code Playgroud) mobile-application typescript angular-directive ionic3 angular