简体   繁体   English

单击父子组件之外的按钮时,如何将数据父组件发送到子组件?

[英]How to send data parent component to child component when click the button which is in outside both parent and child?

I dont know that this way is right.我不知道这种方式是否正确。 But this is my try......So will explain step by step what i want to do.但这是我的尝试......所以将逐步解释我想要做什么。 As a well as these are differant component but last two (Bank-Data.componen.ts/Bank-Data.componen.html) is not different component.这些也是不同的组件,但最后两个(Bank-Data.componen.ts/Bank-Data.componen.html)不是不同的组件。 Button/DB-data/Bank-Data different component Button/DB-data/Bank-Data不同组件

Button.componen.html Button.componen.html

   <button>Click here</button>

DB-Data.componen.ts DB-Data.componen.ts

@Output() accNo= new EventEmitter<any>();

async onSubmit(cusData): Promise<any>{
 //Here is some data come from database......
   this.accountNo = '008974629'; //This value want to send to another component!
   this.accNo.emit(this.accountNo );
}

Bank-Data.componen.ts银行数据.componen.ts

class LectureHallComponent implements OnInit {
 public showaccountNo; //bind the accountNo with this variable

 //execute the function and show the accountNo
 getLectureID(event){
 debugger;
 this.showaccountNo= event;
 }
}

Bank-Data.componen.html Bank-Data.componen.html

 AccountNo: <span *ngIf="showaccountNo"><b>{{showaccountNo}}</b></span> //show the AccountNo

I could get the accountNo & emit the account to Bank-data.component.ts if button had located in Bank-data.component.html .如果按钮位于 Bank-data.component.html ,我可以获得 accountNo 并将该帐户发送到Bank -data.component.ts But i didn't understand it to do when click the button in another component.但是当单击另一个组件中的按钮时,我不明白该怎么做。 Then want to get AccountNo and show it in a another component.然后想要获取 AccountNo 并将其显示在另一个组件中。

  1. This this thing that i want to do............这就是我想做的事情…………
  2. when i click the button i want get account no from DB-Data.componen.ts当我单击按钮时,我想从DB-Data.componen.ts获取帐号
  3. Then that account number want to display Bank-Data.componen.html here然后那个账号想在这里显示Bank-Data.componen.html

if you want to share a variable between multiple components, the best way is via Services.如果你想在多个组件之间共享一个变量,最好的方法是通过服务。 put the 'account no' in a service and inject it into each component that needs it, this way all of them share the same variable.将“帐户号”放入服务中并将其注入到需要它的每个组件中,这样它们都共享相同的变量。

to transmit data from one component to another component which don't share child parent relation, you can use the service to update the data and fetch the data.要将数据从一个组件传输到另一个不共享子父关系的组件,您可以使用该服务来更新数据并获取数据。

So here you create one service call it DataService: The code in your data service would have the following lines:因此,您在这里创建了一项服务,称为 DataService:您的数据服务中的代码将包含以下几行:

setData: BehaviorSubject<any> = new BehaviorSubject(false);
data:BehaviourSubject<any>=new behaviourSubject({})
 constructor(){}
  setDataFlag(){
   this.setData.next(true);
  }
  setData(number:any){
      this.data.next(number);
  }

Now in when you click the button call.现在,当您单击按钮调用时。

this.dataService.setDataFlag();

The componet of DB-Data would subscribe to the flag. DB-Data 的组件将订阅该标志。

So add the following line in that component.因此,在该组件中添加以下行。

this.dataService.setDataFlag.subscribe((data)=>{
   if(data)
   this.dataService.setData(this.AccNo)
    })

And in the bankData component you just subscribe the data.在 bankData 组件中,您只需订阅数据。

this.dataService.data.subscribe((data)=>{this.accNo= data})

You specified an @Output event emitter.您指定了一个 @Output 事件发射器。 Does these components have a parent child relationship?这些组件是否有父子关系? If they do then you can have a look here https://angular.io/guide/component-interaction .如果他们这样做,那么您可以在这里查看https://angular.io/guide/component-interaction If they don't, then as the posts above suggest, perhaps you can use behaviour subject to pass the data.如果他们不这样做,那么正如上面的帖子所建议的那样,也许您可以使用行为主题来传递数据。

暂无
暂无

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

相关问题 如何在父组件上单击按钮时将数据从子组件传递到父组件 - How to pass data from child component to parent component when button clicked on parent component 当表单提交按钮位于父组件中时,如何将子组件中的数据导入父组件? - How to get the data from child component into the parent component when the form submit button is in the parent component? 将数据从子组件发送到父组件 - Send Data from Child Component To Parent Component 按下子组件按钮时反应 Redux 如果 websocket 客户端在父级中,如何让 websocket 客户端发送数据? - React Redux when the child component button is pressed how to have the websocket client send data if the websocket client is in the parent? 如何将数据从父组件发送到AngularJs中的子组件 - How to send data from parent component to child component in AngularJs React中如何将数据从子组件发送到父组件 - How to send data from a child component to parent component in React 如何单击父组件中的按钮并访问子组件中的数据? - How do i click a button in parent component and access the data in child component? 将数据从子级发送到父级组件 - Send data from child to parent component 如何通过单击子组件触发父组件DidUpdate - How to trigger parent componentDidUpdate by click in child component 父组件中的Reactjs按钮可在子组件中提交Redux表单 - Reactjs button in Parent component which submits Redux Form in Child Component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM