1

我的组件创建了一个自定义对象数组,其类型我在我的应用程序的其他地方定义,就像这样

export class UserSchool {

    constructor (
        public schoolId: number,
        public yearRange: string,
        public schoolName: string
    ) {
        this.schoolId = schoolId;
        this.yearRange = yearRange;
        this.schoolName = schoolName;
    }
} 

现在在我的组件中,我运行 onInit 方法,与 REST 服务对话并推送到我的对象数组,就像这样......

// imports are here and correct... didn't want to add here incase too long
@Component({
    selector: 'profile',
    template: require('app/components/profile/profile.html')
})
export class Profile implements OnInit { 
        userPhotos: any;
        userInfo: any;
        gapageName: string;
        albums: string[];
        userData: any;
        userSchools: UserSchool[];

        constructor(private userData:UserData) {
            this.userSchools = [];
        }

        ngOnInit() {
            Observable.forkJoin([
                this.userData.getUserPhotos('123456'),
                this.userData.getUserInfo()]).subscribe(t=> {
                this.userPhotos = t[0];
                this.userInfo = t[1];
                this.gapageName = this.userPhotos.gapageName;
                this.userInfo.data[0].items[0].entries.forEach( (a) => {
                    this.userSchools.push(a); // this populated by two objects which I can log to the console!
                });
            });
        }
    }

一切都很酷,现在在我的模板中,我希望将我的数据输出到ngFor

<div *ngFor="#school of userSchools; #i = index">
    {{i}} - {{ school.yearRange }}, {{ school.schoolName }}
</div>

索引是输出的,没有任何内容{{ school.yearRange }}, {{ school.schoolName }}。DOM 中也没有任何内容,当我应该将其更改为{{ school }}以下内容时写入 DOM: [object Object]我假设这些项目不是 JSON,但是当我写入控制台时,这些项目是 JS 对象,因为它是school. 有什么想法我可能做错了吗?

4

0 回答 0