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

Time:07-12

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.

CodePudding user response:

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;
  • Related