acq-ng-metadata
Version:
Angular 2 decorators and utils for Angular 1.x
112 lines (79 loc) • 1.93 kB
Markdown
# Service
Service is just pure `ES2015 class` with `` decorator, registered via `/` `providers/viewProviders` metadata or legacy `angular.service`(deprecated).
**ES5**
```js
// user.service.js
angular.module('user')
.service('userSvc',User);
User.$inject = ['$http'];
function User($http){
this.hobbies = [];
this.addHobby=function(name){
this.hobbies.push(name);
}
this.getInfo = function(){
return $http.get('/foo/bar/info')
.then(function(response){
return response.data;
});
}
}
```
```js
// user.js
angular.module('user',[]);
```
**TS + ng-metadata**
```typescript
// user.service.ts
import { Inject, Injectable } from 'ng-metadata/core';
export class UserService{
hobbies: string[] = [];
constructor( private $http: ng.IHttpService ){}
addHobby(name: string){
this.hobbies.push(name);
}
getInfo(){
return this.$http.get('/foo/bar/info')
.then((response)=>response.data);
}
}
```
```typescript
// user.component.ts
import { Component } from 'ng-metadata/core';
import { UserService } from './user.service';
export class UserComponent{}
```
---
**(DEPRECATED) TS + ng-metadata**
```typescript
// user.service.ts
import { Inject, Injectable } from 'ng-metadata/core';
export class UserSvc{
hobbies: string[] = [];
constructor( private $http: ng.IHttpService ){}
addHobby(name: string){
this.hobbies.push(name);
}
getInfo(){
return this.$http.get('/foo/bar/info')
.then((response)=>response.data);
}
}
```
```typescript
// user.ts
import * as angular from 'angular';
import {provide} from 'ng-metadata/core';
import {UserSvc} from './user.service';
angular.module('user',[])
.service( ...provide(UserSvc) );
```