When creating a service, need to inject the sercive into the bootstrap ():
Import {bootstrap, Component, View} from "Angular2/angular2""./todoinput"; "./todoservice" ; @Component ({ selector:' app '}) @View ({ directives: [Todoinput], Template: ' <div ><todo-input></todo-input></div> '}) class app{}Bootstrap (APP, [Todoservice]) ;
Todoservice.js
Export class todoservice{ = []; 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