我正在學(xué)習(xí) Angular,我從 Visual Studio 中名為 Angular5TF1 的示例項(xiàng)目開(kāi)始。示例中有一個(gè)名為“獲取數(shù)據(jù)”的菜單選項(xiàng),它模擬從服務(wù)器檢索數(shù)據(jù)。我復(fù)制了這個(gè)方法及其類(lèi)和頁(yè)面,并試圖讓它從我寫(xiě)的方法中檢索數(shù)據(jù) - 它不起作用..我從示例中復(fù)制了“FetchData”方法及其類(lèi)和頁(yè)面,并試圖讓它從我編寫(xiě)的方法中檢索數(shù)據(jù)。返回值顯示正確的值,但當(dāng)我嘗試顯示數(shù)組中的值之一時(shí),它返回未定義。此外,我返回 10 個(gè)值,for 循環(huán)打印 10 行,但它們都是空白的。有人可以解釋原因并幫助我完成這項(xiàng)工作嗎?提前致謝。從組件 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;}
為什么這個(gè)數(shù)據(jù)不顯示
哆啦的時(shí)光機(jī)
2022-11-21 21:32:12