如何将一个函数值传递给另一个 - angular 2 服务
how to pass one function values to another in - angular 2 service
需要帮助,将数据传递给 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
}
);
编辑:
正如承诺的那样,我在 Angular 2/4 中编写了更多关于如何处理 async
操作的示例。
这是一个 plunkr link:
- 第一个示例很简单
Say hello
您在构造函数中 subscribe
并在单击按钮时发出值
- 在第二个例子中,我们创建了一个新的发射器,因为 plunkr 没有让我正确地导入
Observable
,而且我没有太多时间写这个,然后我们订阅并监听来自服务器,当它到达时,我们首先从 JSON
解析它,然后提醒它的属性和值 来处理它
第二个例子是你应该如何在你的服务中处理服务器响应,有很多现有的例子,比如官方 angular 页面上的例子:
Tour of Heroes Angular Tutorial
请注意,每个 AJAX
请求都应在 Angular 中处理。
需要帮助,将数据传递给 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
}
);
编辑:
正如承诺的那样,我在 Angular 2/4 中编写了更多关于如何处理 async
操作的示例。
这是一个 plunkr link:
- 第一个示例很简单
Say hello
您在构造函数中subscribe
并在单击按钮时发出值 - 在第二个例子中,我们创建了一个新的发射器,因为 plunkr 没有让我正确地导入
Observable
,而且我没有太多时间写这个,然后我们订阅并监听来自服务器,当它到达时,我们首先从JSON
解析它,然后提醒它的属性和值 来处理它
第二个例子是你应该如何在你的服务中处理服务器响应,有很多现有的例子,比如官方 angular 页面上的例子:
Tour of Heroes Angular Tutorial
请注意,每个 AJAX
请求都应在 Angular 中处理。