[Angular 2] Inject Service

TypeScript is used heavily as we build up our application, but TypeScript isn’t required. If you want to Inject a Service without using TypeScript, you’ll need to understand the @Inject decorator.

 

import {Component, View, Inject} from "angular2/angular2";
import {TodoService} from "./todoService";

@Component({
    selector: 'todo-input'
})

// Define a ref by using xxx-YYY
// Reference a ref by using xxxYyy
@View({
    template: `
        <input type="text" #log-me />
        <button (click)="onClick($event, logMe.value)">Log Input</button>
    `
})
export class TodoInput{
    todoService;
    constructor(
      //  public todoService:TodoService  //pulbic make todoService global available for the class
        @Inject(TodoService) todoService;
    ){
        console.log(todoService);
    }

    onClick(event , value){
        this.todoService.addTodo(value);
        console.log(this.todoService.todos);
    }
}

 

posted @ 2015-10-23 15:28  Zhentiw  阅读(378)  评论(0编辑  收藏  举报