我正在學習 Angular,我從 Visual Studio 中名為 Angular5TF1 的示例項目開始。示例中有一個名為“獲取數據”的菜單選項,它模擬從服務器檢索數據。我復制了這個方法及其類和頁面,并試圖讓它從我寫的方法中檢索數據 - 它不起作用..我從示例中復制了“FetchData”方法及其類和頁面,并試圖讓它從我編寫的方法中檢索數據。返回值顯示正確的值,但當我嘗試顯示數組中的值之一時,它返回未定義。此外,我返回 10 個值,for 循環打印 10 行,但它們都是空白的。有人可以解釋原因并幫助我完成這項工作嗎?提前致謝。從組件 html:<h1>Interests</h1><div class="col-lg-12"> </div><div class="col-lg-10"> Interest: <input type="text" id="txtInterest" /> <button (click)="test()">Add...</button></div><div class="col-lg-12"> </div><div class="col-lg-12"> </div><div class="col-lg-12"> <p *ngIf="!personalInterests"><em>Loading Interests, Please Wait...</em></p></div><table class='table' *ngIf="personalInterests"> <thead> <tr> <th>Interest Name</th> </tr> </thead> <tbody> <tr *ngFor="let interest of personalInterests"> <td>{{ interest.InterestName }}</td> </tr> </tbody></table>從組件import { Component, Inject } from '@angular/core';import { Http } from '@angular/http';//import { forEach } from '@angular/router/src/utils/collection';@Component({ selector: 'interests', templateUrl: './interests.component.html'})export class InterestsComponent { public personalInterests: PersonalInterest[]; constructor(http: Http, @Inject('BASE_URL') baseUrl: string) { http.get(baseUrl + 'api/Interest/Interests').subscribe(result => { this.personalInterests = result.json() as PersonalInterest[]; }, error => console.error(error)); } public test() { alert(this.personalInterests[1].InterestName); }}interface PersonalInterest { InterestName: string;}
添加回答
舉報
0/150
提交
取消