在输入时将参数传递给Angular 4指令

时间:2017-09-19 14:09:00

标签: angular angular2-directives

我有一个像这样的输入文本字段

<input type="text" class="form-control"  [inputTextFilter]="A" [ngModel]="name">

我的指示是:<​​/ p>

import { Directive, Input, HostListener } from '@angular/core';

@Directive({
  selector: '[inputTextFilter]'
})

export class InputTextFilterDirective {
  @Input('inputTextFilter') params: string;

  @HostListener('keypress', ['$event'])
  onKeyUp(event: KeyboardEvent) {
    console.log('got parameters: '+this.params);
  }
}

我创建了一个名为“inputTextFilter”的指令,我想传递“A”参数。我传递的参数始终显示为未定义。

3 个答案:

答案 0 :(得分:9)

试试这个。

更新

import {Directive, SimpleChanges} from '@angular/core';

@Directive({
  selector: '[inputTextFilter]'
})
export class MyDirective {
  @Input('inputTextFilter') params: string;
  constructor(){}
  ngOnInit(){
     console.log(this.params)
  }
}

答案 1 :(得分:3)

在指令中尝试这样:

if (!message.member.voiceState.channelID)
  return message.channel.createMessage(`:exclamation: You have to join the voice channel to stop the stream.`);
else if (client.leaveVoiceChanne == null)
  return message.channel.createMessage(`:exclamation: No Stream Playing.`);
client.leaveVoiceChannel(message.member.voiceState.channelID);
message.channel.createMessage(`:stop_button: Stopped! Thanks for tuning in :wave:`);

答案 2 :(得分:0)

希望这有助于其他人......问题出在模板中。

当我将输入作为[myDirective] =“A”传递时,A被解释为未定义的变量。因为我想通过字母A我应该说[myDirective] =“'A'”