简体   繁体   English

Angular 6-在组件之间传递数据

[英]Angular 6 - passing data between components

I am learning angular 6 and typescript and have some problems with passing data between multiple components. 我正在学习angular 6和Typescript,并且在多个组件之间传递数据时遇到一些问题。 It looks like on "expense-box.component.ts" component "this.participants" in ngOnInit method is null/empty. 看起来在ngOnInit方法中的“ expense-box.component.ts”组件“ this.participants”为空/空。 So I am not getting any data. 所以我没有任何数据。

First (parent) component - expense.component.ts : 第一个(父)组件-Expense.component.ts

public participants = new Array<Participant>();

getParticipants(): void {
    this.participantService
      .getParticipants(token)
      .subscribe(participants => (this.participants = participants));
}

Participant service - participant.service.ts (to show whats inside getParticipants method) 参与者服务- participant.service.ts(对内部getParticipants方法什么)

  getParticipants(token: string): Observable<Participant[]> {
    return this.http.get<Participant[]>(`${this.url}/${token}`).pipe(
      tap(() => this.log(`Participants assigned to trip with token: ${token} fetched`)),
      catchError(this.handleError<Participant[]>('getParticipants'))
    );
  }

It's html template - expense.component.html : 这是html模板-fee.component.html

<app-expense-box *ngIf="selectedSplitTab == 1" [participants]="participants"></app-expense-box>

expense-box.component.ts 交通费box.component.ts

export class ExpenseBoxComponent implements OnInit {
  @Input() participants: Participant[];
  public equallyList = new Array<Equally>();

  ngOnInit() {
    this.participants.forEach(element => {
      this.equallyList.push(new Equally(element.id, false, element.name));
    });
  }

  addExpense(): void {
    this.expense.equallyTab = this.equallyList;
    this.expenseService.addExpense(this.expense).subscribe(() => console.log('Expense added!'));
  }
}

It's html template - expense-box.component.html 这是html模板- 费用-box.component.html

<app-split-differently [equallyList]="equallyList" [splitEqually]="splitEqually"></app-split-differently>

It's child component - split-differently.component.ts 它是子组件-split-differently.component.ts

export class SplitDifferentlyComponent implements OnInit {
  @Input() splitEqually: boolean;
  @Input() equallyList: Equally[];

  public selectedSplitTab = 1;

  constructor() {}

  ngOnInit() {}

  enableSplitTab(selectedTab: number): void {
    this.selectedSplitTab = selectedTab;
  }
}

It's html template - split-differently.component.html 这是html模板-split-differently.component.html

<div class="form-check" *ngFor="let item of equallyList">
  <input class="form-check-input" type="checkbox" value="" id="equallyTabParticipant{{item.userId}}" checked="{{item.checked}}">
  <label class="form-check-label" for="equallyTabParticipant{{item.userId}}">
    {{ item.userName }}
  </label>
  <hr>
</div>

In the end I want to have current state of EquallyList while sending it to the backend. 最后,我希望在将其发送到后端时具有EquallyList的当前状态。 Now nothing is even displayed, because I am doing something wrong (console not logging any errors). 现在什至没有显示任何内容,因为我做错了事(控制台未记录任何错误)。

Thanks to the answers in comments everything is displayed correctly now. 多亏了评论中的答案,所有内容现在都可以正确显示。 I've checked one checkbox (other are unselected), clicked save button and all "checked" are set to false so values not binded properly. 我已经选中了一个复选框(未选中其他复选框),单击了保存按钮,并且所有“已选中”都设置为false,因此值未正确绑定。 Here is screen with json which is sent to back-end: 这是带有json的屏幕,该屏幕发送到后端:

在此处输入图片说明

Your input might not received the data when you are trying to access it in ngOnInit() method. 当您尝试通过ngOnInit()方法访问数据时,您的输入可能未收到数据。

You should use the setter method for your input and then do your stuff in that method. 您应该使用setter方法进行输入,然后使用该方法进行处理。 Alternatively you can use ngOnChanges() hook 或者,您可以使用ngOnChanges()挂钩

for ex: with setter 例如:带塞特犬

@Input() myInput: any;

 set myInput() {
    //Do stuff here
}

with ngOnchanges 与ngOnchanges

ngOnChanges(changes: SimpleChange){
if(changes['myInput']){
//Do stuff here
}
}

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

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