3

I am using same component for edit as well as add new entries.

if(this.action='edit'){
    this._PersonnelService.getUsersById(this.selectedperid)
      .subscribe((userdata: Iuser) => { this.personnel = userdata[0]; this.personnel.perid = this.selectedperid;console.log(userdata[0]); }, (error) => {
        this.statusMessage = 'Problem with service. Please try again.'
        console.error(error);
      });
   }
    else{     this.personnel=null}

  }

Which means that if the this.action variable is add this.personnel will be set to null.

Now in HTML I have elements such as :

<input type="text"   class="form-control" name="PerFullName"  
[(ngModel)]="personnel.perfullname" id="fullname ">

I am getting truck load of errors in console. My textboxes aren't visible.
How do I fix this?

2

1 Answer 1

7

Use the elvis operator ? in your html

<input type="text"   class="form-control" name="PerFullName"  
[ngModel]="personnel?.perfullname" (ngModelChange)="personnel?.perfullname personnel?.perfullname = $event : null" id="fullname ">

and the ?? in your javascript to set defaults incase of null

const j = null
y = j ?? 'Anonymous'

And this was answered previously here two-way-binding-with-elvis-operator.

Sign up to request clarification or add additional context in comments.

4 Comments

As far as I know. Elvis (?) operator does not work with two way binding
The elvis operator does work when you use [(ngModel)] as [ngModel]
but isnt [ngModel] used for one way binding?
it would be great if someone could explain (ngModelChange)="personnel?.perfullname personnel?.perfullname = $event : null" part

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.