首页 > 其他 > 详细

[Angular 2] Inject Service

时间:2015-10-23 16:25:23      阅读:232      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!