2016-12-06 4 views
-2

こんにちは私は角2で新しいです。私は解決策を用意しました。 index.html、hello_world.html、hello_world.tsに3つのファイルがあり、3つのファイルのコードが以下に示されています。私は親切に私は、角2角度2を使ってサービスを投稿するには?

Index.htmlと

<!DOCTYPE html> 
<html> 
<head> 
<title>Angular 2 QuickStart</title> 
<script 
src="https://rawgithub.com/systemjs/systemjs/0.19.6/dist/system.js"></script> 

<script src="https://code.angularjs.org/tools/typescript.js"></script> 

<script src="https://code.angularjs.org/2.0.0-beta.0/angular2- 
polyfills.js"></script> 

<script src="https://code.angularjs.org/2.0.0-beta.0/Rx.js"></script> 

    <script src="https://code.angularjs.org/2.0.0-beta.0/angular2.dev.js"></script> 

    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/> 

<!-- 2. Configure SystemJS --> 
<script> 
    System.config({ 
    transpiler: 'typescript', 
    typescriptOptions: { emitDecoratorMetadata: true }, 
    packages: {'src': {defaultExtension: 'ts'}} 
    }); 
</script> 

<!-- 3. Bootstrap --> 
<script> 
    System.import('angular2/platform/browser').then(function(ng){ 
    System.import('src/hello_world').then(function(src) { 
     ng.bootstrap(src.HelloWorld); 
     }); 
    }); 
</script> 

</head> 

    <!-- 4. Display the application --> 
    <body> 
    <hello-world>Loading...</hello-world> 
    <!-- <click-me > Click</click-me>--> 
    </body> 
    </html> 

hello_world.html

<div class="container"> 
    <h2>Registration form</h2> 
    <form class="form-horizontal"> 
    <div class="form-group"> 
    <label class="control-label col-sm-2" for="email">First Name:</label> 
    <div class="col-sm-10"> 
    <input type="text" [(ngModel)]="yourFName" class="form-control" placeholder="Enter firstname"> 
    </div> 
</div> 
    <div class="form-group"> 
    <label class="control-label col-sm-2" for="email">Last Name:</label> 
    <div class="col-sm-10"> 
    <input type="text" [(ngModel)]="yourLName" class="form-control" placeholder="Enter lastname"> 
    </div> 
</div> 
    <div class="form-group"> 
    <label class="control-label col-sm-2" for="email">Email:</label> 
    <div class="col-sm-10"> 
    <input type="email" [(ngModel)]="yourEmail" class="form-control" placeholder="Enter email"> 
    </div> 
</div>  
<div class="form-group"> 
    <label class="control-label col-sm-2" for="pwd">Password:</label> 
    <div class="col-sm-10"> 
    <input type="password" [(ngModel)]="yourpwd" class="form-control" id="pwd" placeholder="Enter password"> 
    </div> 
</div> 
<div class="form-group"> 
    <div class="col-sm-offset-2 col-sm-10"> 
    <div class="checkbox"> 
     <label> 

      <input type="checkbox" [ngModel]="rememberMe" (ngModelChange)="addProp($event)"> Remember me</label> 
    </div> 
    </div> 
</div> 
<div class="form-group"> 
    <div class="col-sm-offset-2 col-sm-10"> 

    <button type="submit" (click)="onClickMe($event)" class="btn btn-default">Submit</button> 
    </div> 
    </div> 
    </form> 
    </div> 

hello_worldを使用してデータを投稿することができますどのように私を助けて、角度2からのデータを投稿したいです.ts

import {Component} from 'angular2/core'; 
    import { Http, Response } from '@angular/http'; 
    @Component({ 
    selector: 'hello-world', 
    templateUrl: 'src/hello_world.html'  
    }) 

    export class HelloWorld { 
    yourFName: string = ''; 
    yourLName: string = ''; 
    yourEmail: string = ''; 
    yourpwd: string = ''; 
    rememberMe: string=''; 
    clickMessage = ''; 
    public users = [ 
    { name: 'Jilles',Salary:57000, age: 30 }, 
    { name: 'Todd',Salary:65000, age: 30 }, 
    { name: 'Lisa',Salary:91000,age: 36} 
    ]; 
    onClickMe($event) {  
    alert('Click is working') 
    } 
    } 
+0

チェックangular.ioサイトは、それはあなたがそれを行う方法を示しています... https://angular.io/docs/ts /latest/guide/server-communication.html – btinoco

答えて

1

観測を使用して、HTTP呼び出しを行うためのサービスを作成します....

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

@Injectable() 
export class HeroService { 
    private heroesUrl = 'app/heroes'; // URL to web API 

    constructor (private http: Http) {} 

    addHero (name: string): Observable<Hero> { 

     let options = new RequestOptions({ headers: headers }); 

     return this.http.post(this.heroesUrl, { name }, options) 
       .map(this.extractData) 
       .catch(this.handleError); 
     } 

     private extractData(res: Response) { 
     ...code here for good response... 
     } 

     private handleError (error: Response | any) { 
      ...code here for bad response... 
     } 
} 

は、(追加)新しいヒーローを自分のコンポーネントにサービスを追加し、投稿するaddHero関数を呼び出します。サービスファイルの上にサービスをインポートするようにしてください。あなたがここで多くを学ぶことができる

constructor (private heroService: HeroService) {} 

addHero (name: string) { 
     if (!name) { return; } 

     this.heroService.addHero(name) 
       .subscribe(
       hero => this.heroes.push(hero), 
       error => this.errorMessage = <any>error); 
     } 

}:https://angular.io/docs/ts/latest/guide/server-communication.html

関連する問題