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); } }
时间: 2024-12-14 02:06:54