Angular 从订阅类型脚本返回值
如何从Angular 从订阅类型脚本返回值,angular,typescript,rxjs,angular2-observables,Angular,Typescript,Rxjs,Angular2 Observables,如何从Save()方法返回成功 public SaveItem() { if(save()){ // The goal is to use save method like this // Close pop up; } public SaveAndNew() { if(save()){ // The goal is to use save method like this // Create new item; } private save() { let issucce
Save()
方法返回成功
public SaveItem() {
if(save()){ // The goal is to use save method like this
// Close pop up;
}
public SaveAndNew() {
if(save()){ // The goal is to use save method like this
// Create new item;
}
private save() {
let issuccess = false;
this.myservice.AddParty(newUserObject)
.subscribe(data => {
if (data['status'].toString() === '1') {
return issuccess = false;
} else {
return issuccess = true;
}
},
(er) => {
return issuccess = false;
});
}
- 如果我有
它将抛出一个错误,save():boolean
如果返回必须返回一个值
在订阅之外,它将始终返回一个假值issucess
var isSuccess = await SaveAsync(party);
在这种情况下,您可以使用promise
而不是subscribe
。但是,在将数据绑定到html时,您应该使用
所以,你的服务应该是
AddParty(newUserObject) {
return this.http.post(url)
.toPromise().then(responce => <any[]>responce.json())
.catch(error => {
return error;
});
}
像这样的怎么样:
public SaveItem() {
const isNew = false;
save(isNew)
}
public SaveAndNew() {
const isNew = true;
save(isNew)
}
private save(isNew) {
this.myservice.AddParty(newUserObject)
.subscribe(data => {
if (data['status'].toString() === '1') {
saveComplete(isNew);
} else {
saveFailed(isNew)
}
},
(er) => {
saveFailed(isNew)
});
}
saveComplete(isNew) {
// Check isNew as needed.
// Close dialog or whatever
}
saveFailed(isNew) {
// do whatever here
}
或者。。。TypeScript现在支持异步/等待。。。所以你可以考虑在这里使用它们。有关更多信息,请参见此项:您可以使save方法返回一个可观察的布尔值
public SaveAndNew() {
this.save().subscribe(success =>
{
if(success)
{
// Create new item;
}
});
private save() : Observable<boolean> {
return this.myservice
.AddParty(newUserObject)
.map(data=> data['status'].toString() === '1')
.catch(err => Observable.of(false));
}
public SaveAndNew(){
this.save().subscribe(成功=>
{
如果(成功)
{
//创建新项目;
}
});
private save():可观察{
返回此.myservice
.AddParty(newUserObject)
.map(数据=>data['status'].toString()='1')
.catch(err=>Observable.of(false));
}
像这样试试看
public SaveItem() {
if(save()){ // The goal is to use save method like this
// Close pop up;
}
public SaveAndNew() {
if(save()){ // The goal is to use save method like this
// Create new item;
}
private async save() {
let issuccess = false;
await this.myservice.AddParty(newUserObject)
.subscribe(data => {
if (data['status'].toString() === '1') {
return issuccess = false;
} else {
return issuccess = true;
}
},
(er) => {
return issuccess = false;
});
return issuccess ;
}
在er action之后的subscribe()中有另一个action参数,它将帮助您在不使用observable的情况下返回
public SaveItem() {
if(save()){ // The goal is to use save method like this
// Close pop up;
}
public SaveAndNew() {
if(save()){ // The goal is to use save method like this
// Create new item;
}
private save() {
let issuccess = false;
this.myservice.AddParty(newUserObject)
.subscribe(data => {
if (data['status'].toString() === '1') {
issuccess = false;
} else {
issuccess = true;
}
},
(er) => {
issuccess = false;
},
() => {return issuccess });
}
包含3个参数
subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void):
您可以转换为承诺..移动到承诺对我来说不是一个选项,我的大部分代码都是订阅模式。感谢您的选项出现错误,说类型“()=>Observable”上不存在save``属性“subscribe”`我编辑了我的帖子,应该是这个。save()saveIt不是因为它,它返回一个承诺不是一个可观察的错误,什么是返回一个承诺?操作符在其中使用什么,你有任何文档吗
subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void):