[Angular 2] Inject Service

Source: Internet
Author: User
Tags export class

TypeScript is used heavily as we build to our application, but TypeScript isn ' t required. If you want to Inject a Service without using TypeScript, you'll need to understand the decorator @Inject .

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.