ディレクティブが適用されているコントロール(入力)がクリックされたときにカスタムnumpadを表示するディレクティブを作成しようとしています。 私はIonic 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>
テンキー成分はページの下部に、DOMであるmyPage.html。
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の使用について疑問を抱いていました... そして、私が指示したように、指示がコントロールから簡単に入力できるようになったことはわかりませんでした。 しかし、なぜそれが動作するのか分かりません。 私はディレクティブの機能をより深く理解しなければなりません... –