如何将一个函数值传递给另一个in-angular 2服务
问题描述:
需要帮助,至将角色2应用程序中的数据传递给另一个函数。如何将一个函数值传递给另一个in-angular 2服务
在这个应用中我从服务器获取数据
问题:无法将数据发送到一个功能到另一个功能service.ts
预计:获取价值和阅读它在第二个功能。
请注意:由于它是一个服务器客户端集成我没有一个plunker或jsfiddle。
第一功能
getDLFolders() {
return this.http.get(this.url + '/alfresco/api/-default-/public/cmis/versions/1.1/browser/root/sites/' + this.targetSite + '/documentLibrary/' + this.targetFolder + '?/cmisselector=children&succinct=true&alf_ticket=' + this.ticket)
.map(res => res.json())
.subscribe(
resGetRecords => {
//get objects from document folders
let objectGroup = resGetRecords.objects;
let objectIDs = [];
for (var i = 0; i < objectGroup.length; i++) {
//push object ID's from folder
objectIDs.push(objectGroup[i].object.succinctProperties["cmis:objectId"]);
}
return objectIDs;
//this will return []array with following values
//5645cf45-9b6c-4ad2-ad18-91d24c31f837;1.0
//4712dc17-0c9f-439f-8577-0c80e52d7afc;1.0
}
);
}
二级功能
getJson() {
//how to get the getDLFolders function return values
for (var i = 0; i < objectIDs.length; i++) {
return this.http.get(this.url + '/alfresco/api/-default-/public/cmis/versions/1.1/browser/root?objectId='+ objectIDs[i] +'&alf_ticket=' + this.ticket)
.map(res => res.json());
}
}
在另一个文件
export class UserdashboardComponent implements OnInit {
details = [];
records = [];
errorMsg: string;
constructor(private _myRecords: AuthService, private _myDocuments: AuthService) { }
ngOnInit() {
//get user details,
this._myRecords.getPeople()
.subscribe(
resGetRecords => {
// first name, last name
this.details = resGetRecords;
},
resRecordsError => this.errorMsg = resRecordsError
);
//get document Libary records of user ,
this._myDocuments.getJson()
.subscribe(
resGetRecords => {
//debugger;
// folders
this.records = resGetRecords;
console.log(this.records);
},
resRecordsError => this.errorMsg = resRecordsError
);
}
}
答
订阅您滥用了subscribe
方法。它没有return
的值,因为它是asynchronous
但这里不是Subscription
对象是在使用一个例子:
// this returns the Observable<Response> object
httpGet() {
return this.http.get('url');
}
// lets just print it out for now with 2nd function
secondFunction(data) {
console.log(data);
}
// code inside 'subscribe' will execute at a later time when you receive
// the response from the server hence it won't return the value like a normal function
this.httpGet().subscribe(
(response: Response) => {
console.log(response); // this will still work
return response; // this won't work
}
);
// if you wish to use the response data you need to call the 2nd function from
// within the 'subscribe' method
this.httpGet().subscribe(
(response: Response) => {
secondFunction(response); // send data to 2nd function and print it there
}
);
编辑:
正如所承诺的,我写了关于如何在处理async
操作更多的例子角2/4。
这里有一个plunkr link:
- 第一个例子是简单
Say hello
,你subscribe
在构造函数中,放出按钮值单击第二个例子 - 我们创建了一个新的发射,因为plunkr没有让我导入
Observable
正确,我没有太多时间写这个,然后我们订阅并听取来自服务器的假响应,当它到达时,我们通过首先解析它从JSON
解析它,然后提醒它的属性和值
第二个例子是,你应该如何处理服务器响应你的服务,还有就是很多像官方角度页面上的那些现有的例子:
Tour of Heroes Angular Tutorial
请注意,这是多么的每一个AJAX
请求应该在Angular中处理。
谢谢你的回答。 我花了很多时间理解这一点,答案看起来很清楚,但是当我用我的代码实现时,它并不清楚。最后,我最终使用本地存储。 –
今天晚些时候我可以让你成为小提琴,如果你使用Angular,你绝对需要学习如何使用Observable。 –
谢谢,这会有很大的帮助。当然,我正在学习。 –