2017-07-25 2 views
1

Wie konvertiert man AngularJS in Angular? Ich habe eine Komponente mit dem Namen ForgotPassword erstellt und habe keine Ahnung, wie ich http.post korrekt an meinen C# -Controller senden kann.AngularJS-Dienst in Angular konvertieren

function (window, angular) { 
"use strict"; 

angular.module("Mobile") 
.service("accountService", ["$http", function ($http) { 
return { 

forgotPassword: function (email, success, error) { 
$http.post("Account/ForgotPassword", { Email: email}).success(success).error(error); 
    } 
    } 
}]); 
})(window, window.angular); 

Antwort

1

Sie müssten eine Serviceklasse

app.serivce.ts

import { Injectable } from '@angular/core'; 
import { Http, Response } from '@angular/http'; 
import { Observable } from 'rxjs/Observable'; 
import 'rxjs/Rx'; 

@Injectable() 
export class AppProvider {  
    constructor(private http: Http) { 
    } 

    public forgotPassword(email: string):Observable<any> { 
    let url:string = 'apiUrl/Account/ForgotPassword'; 
    let body: any = { Email: email } 

    return this.http.post(url, body).map((res:Response) => res.json()); 
    } 
} 

Diese Methode aus Ihrem component.ts

Datei
+0

Meinen Sie, dass ich muss se nicht erstellen Parierte App.Service-Datei? –

+0

@ V. Aliosha ja, du solltest. – Pac0

0

accountComponent.ts genannt würde schaffen

constructor(private accountSer: accountService) 

forgotPasswordButton(){ 
this.accountSer.forgotPassword(value) 
} 

accountService.ts

import { Injectable, ErrorHandler } from '@angular/core'; 
import { Http, Headers, RequestOptions, Response } from '@angular/http'; 


import 'rxjs/Rx'; 



@Injectable() 
export class UserDetailService { 
    public headers: Headers; 
    constructor(private http: Http) { 
     this.headers = new Headers(); 
     this.headers.append('Content-Type', 'application/json'); 
    } 
    forgotPassword(value){ 
    this.http.post('Account/ForgotPassword', JSON.stringify(value), { headers: this.headers }).subscribe();}} 
+0

wie kann ich das tun 'private http: Http' in es6? –

+0

Sie können nicht. ": Http" ist Typoskript-Eingabe. statische Typen sind kein Feature in es6 – XGreen

+0

Also, gibt es eine Möglichkeit, dies ohne TS zu tun? –

1

Erstellen einer Serviceklasse

import {Injectable} from '@angular/core'; 
import {Http, Response, Headers} from '@angular/http'; 
import {Observable} from 'rxjs/Observable'; 
import 'rxjs/add/observable/throw'; 

@Injectable() 
export class accountService { 

    forgotPassword(param): Observable<any> { 
     return this.http.post('Account/ForgotPassword', 
      JSON.stringify(param), 
      {headers: this.headers}) 
      .map(this.extractData) 
      .catch(this.handleError); 
    } 
} 

dann Dienst in die Komponente

import {accountService} '../../services/accountService ' 

rufen Sie Ihren Service von der Komponente importieren

export class ForgotPasswordComponent implements OnInit { 
    constructor(private http: HttpClient , public httpService: accountService) { 


    forgotPassword() { 
     this.busy = this.httpService.forgotPassword() 
      .subscribe(
       data => { Email:this.email }, 
       error => console.log('error'), 
       () => this.forgotPasswordComplete(null) 

      ); 
    } 

    forgotPasswordComplete(result: any) { 
     //alert('error loading http service'); 
    } 
    } 
} 
Verwandte Themen