码迷,mamicode.com
首页 > 其他好文 > 详细

[Angular 2] Inject Service

时间:2015-10-23 16:26:11      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

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);
    }
}

 

[Angular 2] Inject Service

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/4904687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!