Ich frage mich, was ist eine beste Möglichkeit, ein Formular nach Erhalt der Antwort vom Server zu laden. Ich habe Code geschrieben, wo er Daten vom Server bezieht, und in meiner Komponente abonniere ich die Antwort, aber Meine Benutzeroberfläche wird geladen, auch wenn ich die Antwort bekomme.Füllen Formular nach HTTP-Antwort in angular2
Ich möchte diese Komponente zum Hinzufügen und Bearbeiten verwenden.
Komponente:
@Component({
selector: 'gate',
templateUrl: '/public/app/views/gate.html',
directives: [GateFormComponent, StrategyComponent],
providers : [MyService]
})
export class MyComponent {
private id:any;
constructor(private _routeParams:RouteParams, @Inject(MyModel) private myModel,
private myService : MyService) {
}
ngOnInit() {
this.id = this._routeParams.get("id");
if (this.id) {
this.gateDataModel.unique_display_id = parseInt(this.id);
this.myService.loadData(this.id)
.subscribe(response => console.log(response));
}
}
In meiner Komponente, ich lade 2 Komponenten, von denen eine Form aufweist, in die ich habe Daten zu laden, sobald ich die Antwort. Und das alles sollte nur passieren, wenn ich eine ID zur Verfügung habe.
Service:
@Injectable()
export class MyService extends HTTPServices {
constructor(http:Http) {
super(http);
}
loadData(id:number) {
return this.query(url)
.map(res => res.json())
.catch(this.handleError)
}
private handleError(error:Response) {
console.log("Error : ", error);
return Observable.throw(error.text());
}
HTTPServices
export class HTTPServices {
private headers:Headers;
private http:Http;
defaultOptionsArgs:RequestOptionsArgs;
constructor(http:Http) {
this.http = http;
this.headers = new Headers();
this.headers.append('Content-Type', 'application/json');
this.defaultOptionsArgs = {
'headers': this.headers
};
}
create(servicePath:string, model:any, options?:RequestOptionsArgs) {
var url = this.getUrl(servicePath);
var options = options ? options : this.defaultOptionsArgs;
return this.http.post(url, JSON.stringify(model), options);
}
query(servicePath:string, options?:RequestOptionsArgs) {
var options = options ? options : this.defaultOptionsArgs;
return this.http.get(servicePath, options);
}
}
---- ----- Edited
Schließlich konnte ich @CanActivate
hinzufügen und es ist Arbeiten.
@Component({
selector: 'gate',
templateUrl: '/public/app/views/gate.html',
directives: [ROUTER_DIRECTIVES, GateFormComponent, StrategyComponent]
})
@CanActivate(
(next: ComponentInstruction, prev: ComponentInstruction) => {
return new Promise((resolve) => {
let id = next.params["id"];
let injector = ReflectiveInjector.resolveAndCreate([HTTP_PROVIDERS]);
let http = injector.get(Http);
if(id){
http.get(URL)
.subscribe((response) => {
console.log(response)
next.routeData.data["response"] = response;
// continue
resolve(true);
}, (error) => {
resolve(false);
});
} else {
resolve(true);
}
});
}
)
export class MyComponent{
private id:any;
constructor(private _routeParams:RouteParams, @Inject(MyModel) private myModel, routeData: RouteData) {
console.log(routeData.get("response"));
}
}
Die Komponente wird geladen und dann erhalte ich die Antwort
Dank
gehen wir nicht so etwas wie ngResource in Winkel 2 haben, wo ich den Anruf tätigen und die Benutzeroberfläche halten, bis ich die Antwort bekommen? – Anuteja
Derzeit keine 'ngResource'. [CanActivate] (https://angular.io/docs/ts/latest/api/router/CanActivate-decorator.html) sollte in der Lage sein, das Routing zu verzögern, bis eine zurückgegebene 'Promise' aufgelöst ist, aber ich denke, das tut nicht ' t funktionieren derzeit richtig. Der Router wird sowieso überarbeitet. –
AsyncRoute? https://angular.io/docs/js/latest/api/router/AsyncRoute-class.html oder vielleicht AsyncPipe? https://angular.io/docs/ts/latest/api/common/AsyncPipe-class.html – IgorL