我如何确认一个周期内所有可观察对象的执行 - (Angular 2)?
How can I to confirm the execution of all observables in a cycle - (Angular 2)?
在Angular 2中,如何确认"n"个"observables"已经完成:
...
for (var i = 1; i <= this.quantity; i++) {
description = this.prefix.trim() + ' ' + i.toString();
point = new PointModel(description, 'A', this.locationModel.id);
this.point.create(<PointModel>point)
.subscribe(
pointModel => {
Materialize.toast('Se ha guardado el punto \"' + pointModel.description + '\"', 2000);
},
error => {
Materialize.toast('Se ha generado un error: \"' + error + '\"', 2000);
}
);
}
...
谢谢!
应该是这样的:
//create and fill points array
var points:PointModel[] = [];
for (var i = 1; i <= this.quantity; i++) {
description = this.prefix.trim() + ' ' + i.toString();
points.push(new PointModel(description, 'A', this.locationModel.id));
}
Observable.from(points).flatMap(point=>{this.point.create(<PointModel>point)})
.subscribe(
pointModel => {
Materialize.toast('Se ha guardado el punto \"' + pointModel.description + '\"', 2000);
},
error => {
Materialize.toast('Se ha generado un error: \"' + error + '\"', 2000);
},
() => {
Materialize.toast('All points have been created!', 2000);
}
);
我的最终代码是:
for (var i = 1; i <= this.quantity; i++) {
description = this.prefix.trim() + ' ' + i.toString();
points.push(new PointModel(description, 'A', this.locationModel.id));
}
Observable.fromArray(points).flatMap( //'fromArray' is deprecated, instead use 'from'
point => {
return this.point.create(<PointModel>point); //the return was the key here...
}
)
.subscribe(
pointModel => {
Materialize.toast('Se ha guardado el punto \"' + pointModel.description + '\"', 2000);
},
error => {
Materialize.toast('Se ha generado un error: \"' + error + '\"', 2000);
},
() => {
this.loadPointsFromModelId(this.pointModel.id);
}
);
在Angular 2中,如何确认"n"个"observables"已经完成:
...
for (var i = 1; i <= this.quantity; i++) {
description = this.prefix.trim() + ' ' + i.toString();
point = new PointModel(description, 'A', this.locationModel.id);
this.point.create(<PointModel>point)
.subscribe(
pointModel => {
Materialize.toast('Se ha guardado el punto \"' + pointModel.description + '\"', 2000);
},
error => {
Materialize.toast('Se ha generado un error: \"' + error + '\"', 2000);
}
);
}
...
谢谢!
应该是这样的:
//create and fill points array
var points:PointModel[] = [];
for (var i = 1; i <= this.quantity; i++) {
description = this.prefix.trim() + ' ' + i.toString();
points.push(new PointModel(description, 'A', this.locationModel.id));
}
Observable.from(points).flatMap(point=>{this.point.create(<PointModel>point)})
.subscribe(
pointModel => {
Materialize.toast('Se ha guardado el punto \"' + pointModel.description + '\"', 2000);
},
error => {
Materialize.toast('Se ha generado un error: \"' + error + '\"', 2000);
},
() => {
Materialize.toast('All points have been created!', 2000);
}
);
我的最终代码是:
for (var i = 1; i <= this.quantity; i++) {
description = this.prefix.trim() + ' ' + i.toString();
points.push(new PointModel(description, 'A', this.locationModel.id));
}
Observable.fromArray(points).flatMap( //'fromArray' is deprecated, instead use 'from'
point => {
return this.point.create(<PointModel>point); //the return was the key here...
}
)
.subscribe(
pointModel => {
Materialize.toast('Se ha guardado el punto \"' + pointModel.description + '\"', 2000);
},
error => {
Materialize.toast('Se ha generado un error: \"' + error + '\"', 2000);
},
() => {
this.loadPointsFromModelId(this.pointModel.id);
}
);