Angulaire 2: TypeError: l_thing0 est pas défini dans [{{chose.titre}} en AppComponent@4:44]

J'ai une erreur étrange dans mon application. Il est censé imprimer {{thing.title}} à partir d'un objet, mais il affiche une erreur dans la console:

EXCEPTION: TypeError: l_thing0 is undefined in [{{thing.title}} in AppComponent@4:44]

Je ne suis pas sûr de l'endroit où l_thing0 vient de. Si j'essaie de montrer {{thing}}dans la page, il affiche [object Object]. Si j'essaie de JSON.stringify(this.thing) (voir la showObj() fonction), il affiche correctement le stringified objet. Mais si je tente d'accéder à un attribut comme {{thing.title}} je reçois le message d'erreur que l_thing0 est pas défini.

Ici est d'application.composante.ts:

import {Component, OnInit} from 'angular2/core';
import {Thing} from './thing';
import {ThingService} from './thing.service';
import {SubThingComponent} from "./subthing.component";

@Component({
    selector: 'thing-app',
    template: `
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    <h1>{{thing.title}} <a href="#" (click)="showObj()" class="btn btn-default">Show Stringified Obj</a></h1>
                    <subthing></subthing>
                </div>
            </div>
        </div>
    `,
    styles:[`

    `],
    directives: [SubThingComponent],
    providers: [ThingService]
})

export class AppComponent implements OnInit {

    constructor(private _thingService: ThingService) { }

    public thing: Thing;

    showObj() {
        //This correctly shows the stringified object
        alert(JSON.stringify(this.thing));
    }

    getThing() {
        this._thingService.getThing().then(thing => this.thing = thing);
        //This correctly logs the object
        setTimeout(() => {console.log('thing', this.thing)}, 5000);
    }

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

Des idées?

  • Est-ce à {{thing?.title}} fait disparaître l'erreur?
  • Oui, merci. J'ai juste compris. (au bout de 7 heures. :/ )
  • np, essayez de ne pas obtenir utilisé à l'opérateur si, j'ai posté une meilleure solution pour elle.
  • Bonne idée. Merci.
InformationsquelleAutor Josh | 2016-01-16