简体   繁体   中英

Why not get the value passed from the parent component to the child component correctly?

I need to pass a variable value from the parent component to the child component through an input. That is, the screenMode value received through the above ng-container needs to be given to the add-user child component.

<ng-container *ngIf="screenMode == screenStateEnum.ADD_USER || screenMode == screenStateEnum.EDIT_USER">

<add-user [screenMode] = "screenMode" [userId]="selectedRow?.userId" (goToUsers)="refresh()"></add-user>

</ng-container>

In add-user.component has @Input() screenMode: string; But this input always returns 0 for EDIT_USER and 1 for ADD_USER.

These values imply that your screenStateEnum enum is a numeric enum and looks something like this:

enum screenStateEnum {
  ADD_USER,
  EDIT_USER
}

If you want string values, you'd have to use a string enum . That could look something like:

enum screenStateEnum {
  ADD_USER = 'ADD_USER',
  EDIT_USER = 'EDIT_USER'
}

Of course you could also just change the type of your input field from:

@Input() screenMode: string;

to:

@Input() screenMode: screenStateEnum;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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