大家好我在Angular2中遇到的一个大问题是异步通信问题。在下面的代码缓冲区中,在响应到达component.ts之前附加值,我使用的是promises。请仔细阅读以下代码。非常感谢您的帮助
/* component.ts */
ngOnInit() {
let buffer = '';
const tempModel = this.appMenuService.getMenusForUser();
tempModel.then(res => {
this.modelData = JSON.parse( res.toString() );
setTimeout(() => {}, 10000 );
console.log(this.modelData);
});
buffer = buffer + " \n";
buffer = buffer + "{ \n label: 'Institutions In OMS' , icon:'fa fa-fw fa-institution', onOpenIcon:'fa fa-fw fa-institution', \n";
buffer = buffer + "items:[ \n";
for(let i = 0; i < this.modelData.length - 1; i++) {
buffer = buffer + "label: this.translate.instant('"+this.modelData[i]+"'"+", icon: 'fa fa-fw fa-bell-o', routerLink: ['/"+this.modelData[i]+"'] \n },";
}
/* service.ts */
@Injectable()
export class AppMenuService {
user : User = new User();
constructor( private http: HttpService, private cacheservice: CacheService ) { }
getMenusForUser() {
debugger;
this.user.userName = '123';
this.user.passWord = '123';
return this.http.post( 'getMenuItems', this.user ).toPromise()
.then( response => {
return <any[]>response['_body'];
} );
}
}
我想将值附加到&#39;缓冲区&#39;从getMenusForuser()方法获得响应后。但是在响应到达component.ts
之前,值会附加到缓冲区