angular 2: using a service to broadcast an event

后端 未结 2 1044
我在风中等你
我在风中等你 2021-01-31 10:01

I\'m trying to get a button click in one component to put focus on an element on another component. (Frankly, I don\'t understand why this must be so complex, but I have not bee

2条回答
  •  不思量自难忘°
    2021-01-31 10:22

    First of all, use a BehaviorSubject instead of EventEmitter. Change the declaration of skipCliekd to the following:

    skipClicked: BehaviorSubject = new BehaviorSubject(false);
    

    Then, you need to broadcast the new value using next() method as following:

    this.skipClicked.next (true);
    

    Also, change your subscription to:

     this.skipToContent.skipClicked.subscribe( value => {
         if (value === true) {
             console.log("!"); 
             // should put focus() on input 
         }
     });
    

提交回复
热议问题