在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
描述服务是仅负责执行特定任务的JavaScript函数。 角度服务使用依赖注入机制注入,并包括应用程序所需的值,函数或特性。 在Angular中没有什么有关服务,并且没有ServiceBase类,但仍然可以将服务作为Angular应用程序的基础。 例子下面的例子描述了在Angular 2中使用服务: <!DOCTYPE html> <html> <head> <title>Angular 2 Services</title> <!--Load libraries --> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/es6-shim.min.js"></script> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/system-polyfills.js"></script> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/angular2-polyfills.js"></script> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/system.js"></script> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/typescript.js"></script> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/Rx.js"></script> <script src="https://atts.ogeek.cn/attachments/tuploads/angular2/angular2.dev.js"></script> <script> System.config({ transpiler: 'typescript', typescriptOptions: { emitDecoratorMetadata: true }, packages: {'app': {defaultExtension: 'ts'}} }); System.import('/angular2/src/app/service_main') .then(null, console.error.bind(console)); </script> </head> <body> <my-app>Loading...</my-app> </body> </html> 上述代码包括以下配置选项:
要运行代码,您需要以下TypeScript(.ts)文件,您需要保存在应用程序文件夹下。 metadata_main.tsimport {bootstrap} from 'angular2/platform/browser'; //importing bootstrap function import {AppComponent} from "./app_service.component"; //importing component function bootstrap(AppComponent); 现在我们将在TypeScript(.ts)文件中创建一个组件,我们将为该组件创建一个视图。 app_service.component.tsimport {Component} from 'angular2/core'; import {MyListComponent} from "./service-list.component"; @Component({ selector: 'my-app', template: ` <country-list></country-list> `, directives: [MyListComponent] }) export class AppComponent { }
import {Component} from "angular2/core"; import {CountryService} from "./country.service"; import {Contact} from "./country"; import {OnInit} from "angular2/core"; @Component({ selector: "country-list", template: ` List of Countries<br> <ul> <li *ngFor="#cntry of countries">{{ cntry.name }}</li> </ul> `, providers: [CountryService] }) export class MyListComponent implements OnInit { public countries : Country[]; constructor(private _countryService: CountryService) {} getContacts(){ this._countryService.getContacts().then((countries: Country[]) => this.countries = countries); } ngOnInit():any{ this.getContacts(); } }
import {Injectable} from "angular2/core"; import {COUNTRIES} from "./country.contacts"; //@Injectable() specifies class is available to an injector for instantiation and an injector will display an error when trying to instantiate a class that is not marked as @Injectable() @Injectable() //CountryService exposes the getContacts() method that returns the data export class CountryService { getContacts() { return Promise.resolve(COUNTRIES); // takes values from country.contacts typescript file } }country.contacts.ts import {Country} from "./country"; //storing array of data in Country export const COUNTRIES: Country[] =[ {name :"India"}, {name: "Srilanka"}, {name: "South Africa"}, {name: "New Zealand"} ];country.ts export interface Country{ name: string } 输出让我们执行以下步骤,看看上面的代码如何工作:
或,您可以以其他方式运行此文件:
|
请发表评论