简体   繁体   English

fromEvent rxjs Angular 5 工作不正常

[英]fromEvent rxjs Angular 5 is not properly working

I have create radio buttons from an array ie我已经从数组创建单选按钮,即

['Yes','No']
<mat-radio-group class="tt" style="display: inline-block" formControlName="recursive" >
<div class="opt" *ngFor="let t of tarrif_type_arr">
<mat-radio-button value={{t}} name="recursive" #recursive id="recursive" [checked]="checked" >{{t}</mat-radio-button>      
</div>
</mat-radio-group>

And now i want to listen for the click event of my recursive element i tried using现在我想听我尝试使用的递归元素的点击事件

@ViewChild('recursive',{ read: ElementRef }) recur:ElementRef;
ngAfterViewInit()
{
let clickObservable$=fromEvent(this.recur.nativeElement,'click').subscribe(res=>{
  console.log(res);
})
} 

but the issue i am facing is its triggering the event only for first option ie yes whenever i click on Yes it shows但我面临的问题是它仅针对第一个选项触发事件,即是的,只要我单击是,它就会显示

MouseEvent {isTrusted: true, screenX: 2084, screenY: 410, clientX: 498, clientY: 308, …}

but it doesn't even check or console.log when click on No但是当点击 No 时它甚至不检查或 console.log

As @ConnorsFan says you have to use ViewChildren instead of ViewChild .正如@ConnorsFan 所说,您必须使用ViewChildren而不是ViewChild

For example the typed version:例如键入的版本:

@ViewChildren(MatRadioButton) recur: QueryList<MatRadioButton>;
ngAfterViewInit()
{
  this.recur.forEach(c => {
    // here your goal
    fromEvent(c._nativeElement,'click').subscribe(res=>{
      console.log(res);
    });
  })  
} 

or direct interact with ElementRef:或直接与 ElementRef 交互:

@ViewChildren(MatRadioButton, { read: ElementRef }) recur: QueryList<ElementRef>;
ngAfterViewInit()
{
  this.recur.forEach(c => {
    // here your goal
    fromEvent(c.nativeElement,'click').subscribe(res=>{
      console.log(res);
    });
  })  
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM