Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用angular 2发布服务?_Javascript_Angular_Mean Stack - Fatal编程技术网

Javascript 如何使用angular 2发布服务?

Javascript 如何使用angular 2发布服务?,javascript,angular,mean-stack,Javascript,Angular,Mean Stack,嗨,我是angular 2的新手。我准备了一个解决方案。index.html上有三个文件,hello_world.html,hello_world.ts下面给出了三个文件的代码。我想发布angular 2的数据。请帮助我如何使用angular 2发布数据 Index.html <!DOCTYPE html> <html> <head> <title>Angular 2 QuickStart</title> <script s

嗨,我是angular 2的新手。我准备了一个解决方案。index.html上有三个文件,hello_world.html,hello_world.ts下面给出了三个文件的代码。我想发布angular 2的数据。请帮助我如何使用angular 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> 

创建一个服务,使用observables进行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...
      }
}
从'@angular/core'导入{Injectable};
从'@angular/Http'导入{Http,Response};
从“/Hero”导入{Hero};
从“rxjs/Observable”导入{Observable};
@可注射()
导出类服务{
private heroesUrl='app/heroes';//指向web API的URL
构造函数(私有http:http){}
addHero(名称:string):可观察{
let options=newrequestoptions({headers:headers});
返回this.http.post(this.heroesUrl,{name},options)
.map(此.extractData)
.接住(这个.把手错误);
}
私有数据(res:Response){
…在这里输入代码以获得良好的响应。。。
}
私有句柄错误(错误:响应|任意){
…在这里输入错误响应的代码。。。
}
}
然后将服务添加到组件中,并调用addHero函数发布(添加)新的hero。确保在服务文件顶部导入服务

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);
      }
构造函数(私有heroService:heroService){}
addHero(名称:string){
如果(!name){return;}
this.heroService.addHero(名称)
.订阅(
英雄=>这个。英雄。推(英雄),

error=>this.errorMessage=

检查angular.io站点,它向您展示了如何操作。。。
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...
      }
}
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);
      }