[英]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 并将其显示在另一个组件中。
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.