【发布时间】:2016-10-06 02:02:14
【问题描述】:
我想保存和访问 Angular2 对象,但我的值未定义。我正在获取一个对象,但这无法访问,例如数组。我怎样才能将它作为数组?
Node.js api.js
api.get('/getData', function(req, res){
res.send({name:'test'})
});
数据服务 PassProfileDataService.ts
import {Component, Injectable} from '@angular/core'
import { Http} from "@angular/http";
@Injectable()
export class PassProfileDataService {
constructor(private http: Http) {}
getItems(){
return this.http.get('/api/getData').map((res:any) => res);
}
}
使用服务的组件
import {Component, Input, OnInit} from '@angular/core';
import {PassProfileDataService} from '../common/PassProfileDataService';
@Component({
styleUrls:['/assets/css/bootstrap.css', '/assets/css/profile.css'],
selector: "profile",
templateUrl: `client/components/profile/profile.component.html`
})
export class ProfileComponent implements OnInit {
items:any;
constructor(private _sharedService: PassProfileDataService){}
ngOnInit(){
this.items = this._sharedService.getItems();
console.log(this.items + ' test');
}
}
视图组件profile.component.html
<div *ngFor="let i of items">
{{i.name}}
</div>
我收到以下异常:
core.umd.js:3462 例外:找不到“object”类型的不同支持对象“[object Object]”。 NgFor 只支持绑定到 Arrays 等 Iterables。
【问题讨论】:
标签: angular