2016-01-28 23 views
7

Đối phó với vấn đề xa lạ trong góc 2.góc 2 thẻ mẫu nói đối tượng là undefined

Nếu bạn nhìn vào các thành phần dưới đây, các dịch vụ trả về một đối tượng phân tích cú pháp - ví dụ tôi có thể console.log nó. Bạn có thể xem kết xuất được dán dưới dạng nhận xét. Bất cứ khi nào tôi cố gắng sử dụng đối tượng đã nói trong chế độ xem, tôi gặp lỗi: EXCEPTION: TypeError: Cannot read property 'subject' of undefined in [{{theData.subject}} in [email protected]:27].

Điều này không có ý nghĩa với tôi vì tôi có thể thấy đối tượng trong bảng điều khiển tốt. Chạy typeof cũng trả về rằng đó là một đối tượng.

Component Mã

import { Component, View } from 'angular2/core' 
import { CORE_DIRECTIVES } from 'angular2/common' 
import {SubjectService} from "../../services/subject/SubjectService.ts"; 
import Rx from 'rxjs/Rx' 
import { Response } from 'angular2/http' 
import {ROUTER_PROVIDERS} from "angular2/router"; 
import {RouteParams} from "angular2/router"; 


@Component({ 
    selector: 'subjectHomeComponent', 
    providers: [SubjectService] 
}) 

@View({ 
    template: ` 
     Hello World <span>{{theData.subject}}</span> 
    ` 
}) 

export class SubjectHomeComponent{ 
    constructor(subjectService: SubjectService, params: RouteParams){ 
     this.id = params.get('id') 

     subjectService.getSubjectCardsAndNotes(this.id) 
      .subscribe((res: Response) => { 
       this.theData = res 
       console.log(res) //returns {"subject":{"id":1,"name":"Spanish","created_at":"2016-01-23T06:54:50.321Z","updated_at":"2016-01-23T06:54:50.321Z"},"subject_id":1,"notes":[{"id":1,"title":"first note","body":"here i am!","created_at":"2016-01-27T03:10:09.238Z","updated_at":"2016-01-27T03:10:09.238Z","subject_id":1},{"id":2,"title":"first_note","body":"hello _buddy_","created_at":"2016-01-28T20:45:36.044Z","updated_at":"2016-01-28T20:45:36.044Z","subject_id":1}]} 

      }); 
    } 
} 

Mã dịch vụ (Mà tôi không nghĩ là có liên quan nhưng bạn không bao giờ biết)

import {Injectable} from "angular2/core"; 
import {Http, HTTP_PROVIDERS, Response} from "angular2/http"; 
import Rx from 'rxjs/Rx' 
import 'rxjs/add/operator/map'; 

@Injectable() 
export class SubjectService{ 
    constructor(public http: Http){ 

    } 

    getSubject(){ 
     return this.http.request('/subjects.json') 
      .map((response) => response.json()) 
    } 
    getSubjectCardsAndNotes(id){ 
     return this.http.request(`subjects/${id}/subject_cards_and_notes.json`) 
      .map((response) => response.json()) 
    } 
} 
+1

http://stackoverflow.com/questions/34833358/angular-2-typeerror-l-thing0-is-undefined-in-thing -title-in-appcomponent/34833436 # 34833436 – Langley

Trả lời

Các vấn đề liên quan