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

[Angular 2] Use Service use Typescript

时间:2015-10-23 16:12:35      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

When creating a service, need to inject the sercive into the bootstrap():

import {bootstrap, Component, View} from "angular2/angular2";
import {TodoInput} from "./todoInput";
import {TodoService} from "./todoService";

@Component({
    selector:‘app‘
})
@View({
    directives: [TodoInput],
    template: `
        <div><todo-input></todo-input></div>
    `
})
class App{

}

bootstrap(App, [TodoService]);

 

todoService.js

export class TodoService{
    todos: string[] = [];

    addTodo(value: any):void {
        this.todos.push(value);
    }
}

 

inputTodo.js:

import {Component, View} 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{
    constructor(
        public todoService:TodoService  //pulbic make todoService global available for the class
    ){
        console.log(todoService);
    }

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

 

[Angular 2] Use Service use Typescript

标签:

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

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