지시문을 적용한 컨트롤 (입력)을 클릭하면 사용자 지정 numpad를 표시하도록 지시문을 작성하려고합니다. 나는 이오니아 2 RC5에서 일하고있다.이온 2 - 지침에서 구성 요소에 어떻게 도달합니까?
<ion-content> <ion-item> <ion-label stacked>Label</ion-label> <ion-input dirNumpad type="text" [(ngModel)]="myField"></ion-input> </ion-item> </ion-content> <ion-footer> <numpad #idNumpad hidden></numpad> </ion-footer>
숫자 패드 성분 페이지의 하단에서의 mypage.html DOM이다.
dirNumpad.ts
import { Directive, ElementRef, ViewChild } from '@angular/core'; import { Numpad } from '../../components/numpad/numpad'; @Directive({ selector: '[dirNumpad]', // Attribute selector host: { '(click)': 'onClick()' } }) export class DirNumpad { @ViewChild('idNumpad') numpad: Numpad; constructor(private el: ElementRef) { } onClick() { this.showNumpad(); } showNumpad() { console.log(this.numpad); => undefined this.numpad.show(); => error: show property does not exist on undefined } }
numpad.html
<div class="numpad" style="position:absolute; top:auto; left:0; right:0; bottom:0; height:150px;">My Numpad</div>
numpad.ts
import { Component, Input } from '@angular/core'; @Component({ selector: 'numpad', templateUrl: 'numpad.html' }) export class Numpad { constructor() {} }
내 문제 : ViewChild를 통해 지시문 내부에서 숫자판 구성 요소에 연결할 수 없습니다. console.log (this.numpad)는 항상 "undefined"를 반환합니다! 지시문을 적용한 입력을 사용자가 클릭 할 때만 숫자 패드를 표시해야합니다 ...
무엇이 잘못 되었나요? 나는이 문제에 갇혀 있기 때문에 어떤 도움을 주시면 감사하겠습니다.
감사합니다! 완벽하게 작동합니다. 이 특별한 경우에 Viewchild 사용에 대해 몇 가지 의구심이있었습니다 ... 그리고 지시어가 내게 보여준 것처럼 쉽게 컨트롤에서 입력을받을 수 있는지 알지 못했습니다. 그러나 실제로 작동하는 이유를 이해하지 못합니다. 지시어 기능에 대해 더 깊이 생각해야합니다 ... –