Angular2 Child component with ADDITIONAL INPUT

TL; DR: Could it @Inputbe optional?

So, I have this child component that is modal to create a transaction (in banking terms), and I want to update this modal transaction to create or update.

Currently modal is just a group of input fields and a submit button to create. What I want to achieve is when this component receives the transaction as an input, it will be in edit mode, and when it will not, it will be included in the "create mode" (this means that the input field will be filled with data in edit mode and empty in create mode)

Could it @Input transactionbe optional?

+4
source share
1 answer

Yes. Moreover, just do it if you want changes

<div *ngIf="transaction">Some edit stuff</div>
<div *ngIf="!transaction">Create stuff</div>

If you need specific material

ngOnInit() {
  if(!this.transaction) { 
    // create stuff only 
  } else {
    // update stuff only
  }
}

EDIT : how would I do it (in your case)

This may not be the best solution, but should work (if I didn't get the syntax wrong)

ngOnInit() {
  if(!this.transaction) { 
    this.transaction = new Transaction();
  } 
}

this way all class properties will be available, only empty. If you do not want them to be predefined, you can set them in the constructor or in ngOnInit.

+2
source

Source: https://habr.com/ru/post/1654039/


All Articles