में [{{thing.title}} में अपरिभाषित है मुझे अपने ऐप में एक अजीब त्रुटि मिल रही है। यह एक वस्तु से बाहर {{thing.title}}
मुद्रित करने के लिए माना जाता है, लेकिन यह कंसोल में एक त्रुटि दिखाता है:कोणीय 2: TypeError: l_thing0 AppComponent @ 4: 44]
EXCEPTION: TypeError: l_thing0 is undefined in [{{thing.title}} in [email protected]:44]
मुझे यकीन है कि जहां l_thing0
से आ रहा है नहीं कर रहा हूँ। यदि मैं पृष्ठ में {{thing}}
दिखाने का प्रयास करता हूं, तो यह [object Object]
प्रदर्शित करता है। अगर मैं JSON.stringify(this.thing)
(showObj()
फ़ंक्शन देखें) का प्रयास करता हूं, तो यह सही ढंग से स्ट्रिंग किए गए ऑब्जेक्ट को प्रदर्शित करता है। लेकिन अगर मैं {{thing.title}}
जैसी विशेषता तक पहुंचने का प्रयास करता हूं तो मुझे त्रुटि मिलती है कि l_thing0
अपरिभाषित है।
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();
}
}
कोई भी विचार:
यहाँ app.component.ts है?
क्या यह '{{thing? .title}} 'त्रुटि को दूर कर देता है? – Langley
हां, धन्यवाद। मैंने बस यह पता लगाया। (7 घंटों के बाद।: /) – Josh
एनपी, उस ऑपरेटर के लिए उपयोग न करने का प्रयास करें, हालांकि, मैंने इसे बेहतर समाधान पोस्ट किया है। – Langley