1

Having a devil of a time figuring out why this is happening. I've got an A2 project set up using the CLI and I have the following component:

import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

@Component({
  selector: 'app-user-profile-card',
  templateUrl: 'user-profile-card.component.html',
  styleUrls: ['user-profile-card.component.scss']
})
export class UserProfileCardComponent implements OnInit {

  public person;

  constructor(private http: Http) {
  }

  getUserProfile(){
    this.http.get('http://somedomain.com/12345')
        .map(res => res.json())
        .subscribe(
            person => { this.person = person.person },
            err => console.error(err),
            () => console.log(this.person.sex)
        );
  }

  ngOnInit() {
    this.getUserProfile();
  }
}

The console log for this.person.sex shows the correct value, but when I try to bind to it from the template:

<div>{{ person.sex }}</div>

I get the following error:

undefined is not an object (evaluating 'self.context.person.sex')

Any ideas on this?

0

1 Answer 1

3

That's because all http calls are asynchronous operations and you are trying to display person.sex in your template before data arrive. You can use safe navigation operator (?) to "protect" your template until data arrive:

<div>
    {{ person?.sex }}
</div>

You can also use ngIf directive:

<div *ngIf="person">
    {{ person.sex }}
</div>

This way, your div won't appear in DOM until variable person is populated. You can read more about safe navigation operator here and more about ngIf directive here.

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

1 Comment

Wow... Such a simple thing but a huge effect. I used your first example and all is now awesome. Thanks a lot for this!

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.