我想保存并访问Angular2对象,但我得到的值未定义。我得到一个对象,但不能访问,如数组。我怎么能做到阵列?访问Angular2对象?
Node.js的api.js
api.get('/getData', function(req, res){
res.send({name:'test'})
});
的DataService 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 EXCEPTION:无法找到一个不同的支持对象“ [对象]类型的[对象对象]。 NgFor仅支持与阵列等Iterables绑定。
我正在以下错误客户端/组件/配置文件/ profile.component.ts(27,6):错误TS2322:类型“任何[]”是不能分配给输入“可观察”。 [1] client/components/profile/profile.component.ts(27,6):错误TS2322:类型'any []'不可分配给类型'Observable '。 [1]类型'any []'中缺少属性'_isScalar'。另外我怎样才能访问名称属性? –
Tony
@Tony,这是一个更复杂的打字问题,可能值得一个新的问题。你必须展示更多的代码才能看到getUserWishList实际返回的是什么(Observable,但是泛型是什么?即Observable >)以及真正的值是什么。 –
@Tony,你的'console.log'图片在你的问题中看起来像'value'只是一个对象而不是数组。由此我认为价值是一个“任何”,“this.data”不应该是任何形式的可观察的,this.data应该只是一个“任何”。或者更好的是,一个正确定义的界面。 –