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.

main.ts:

import {Component, provide} from 'angular2/core';
import {bootstrap} from 'angular2/bootstrap';
import {TodoService} from './TodoService';
import {TodoInput} from './TodoInput'; @Component({
selector: 'app',
directives: [TodoInput],
template: `
<todo-input></todo-input>
`
}) class App { } // []: we can inject service which can be used for the whole app
// use provide to inject service, when you don't want to use TypeScript
bootstrap(App, [
provide('whatever', {useClass: TodoService})
]);

TodoInput.ts:

import {Component, Inject} from 'angular2/core';
import {TodoService} from './TodoService'; @Component({
selector: 'todo-input',
template: `
<div>
<input #myInput type="text">
<button (click)="onClick(myInput.value)">Click me</button>
</div>
`
}) export class TodoInput{ // If you don't want to use TypeScript
constructor(@Inject('whatever') public todoService){ } onClick(todo: string){
this.todoService.todos.push(todo);
console.log(this.todoService.todos);
}
}

TodoService.ts:

import {Injectable} from 'angular2/core';
@Injectable() // If you don't want to use TypeScript
export class TodoService{
todos = [];
}

[Angular 2] Using the @Inject decorator的更多相关文章

  1. [Angular 2] Value Providers & @Inject

    Dependecies aren’t always objects created by classes or factory functions. Sometimes, all we really ...

  2. angular使用@angular/material 出现"export 'ɵɵinject' was not found in '@angular/core'

    WARNING in ./node_modules/@angular/cdk/esm5/a11y.es5.js 2324:206-214 "export 'ɵɵinject' was not ...

  3. [Angular 2] Inject Service

    TypeScript is used heavily as we build up our application, but TypeScript isn’t required. If you wan ...

  4. Angular源代码学习笔记-原创

    时间:2014年12月15日 14:15:10 /** * @license AngularJS v1.3.0-beta.15 * (c) 2010-2014 Google, Inc. http:// ...

  5. Angular 4+ 修仙之路

    Angular 4.x 快速入门 Angular 4 快速入门 涉及 Angular 简介.环境搭建.插件表达式.自定义组件.表单模块.Http 模块等 Angular 4 基础教程 涉及 Angul ...

  6. Angular指令1

    Angular的指令 也就是directive,其实就是一WebComponent,以前端的眼光来看,好象很复杂,但是以后端的眼光来看,还是非常简单的.其实就是一个中等水平的类. var myModu ...

  7. 【17】进大厂必须掌握的面试题-50个Angular面试

    我们整理了一份主要的Angular面试问题清单,分为三部分: 角度面试问题–初学者水平 角度面试问题–中级 角度面试问题–高级 初学者水平–面试问题 1.区分Angular和AngularJS. 特征 ...

  8. Angular SSR 探究

    一般来说,普通的 Angular 应用是在 浏览器 中运行,在 DOM 中对页面进行渲染,并与用户进行交互.而 Angular Universal 是在 服务端 进行渲染(Server-Side Re ...

  9. Angularjs 源码

    /** * @license AngularJS v1.3.0-beta.15 * (c) 2010-2014 Google, Inc. http://angularjs.org function t ...

随机推荐

  1. .net对文件的操作之文件读写

    读写文件的步骤一般需要5步: 创建文件流 创建读写器 执行读或写的操作 关闭读写器 关闭文件流 需要引用:System.IO这个命名空间 代码演示: string path = @"F:\a ...

  2. tomcat 远程 调试 eclipse

    windows系统: 修改catalina.bat   端口9000 SET CATALINA_OPTS=-server -Xdebug -Xnoagent -Djava.compiler=NONE ...

  3. Swift - IBOutlet返回nil(fatal error: unexpectedly found nil while unwrapping an Optional value)

    在Swift 中 ViewController 默认构造方法不关联同名的xib文件 在使用OC的时候,调用ViewController的默认构造函数,会自动关联到一个与ViewController名字 ...

  4. 轻松完成WAP手机网站搭建

    用PHPCMS最新发布的V9搭建了PHPCMS研究中心网站(http://phpcms.org.cn)完成后,有用户提出手机访问的问题,于是着手搭建WAP无线站(wap.phpcms.org.cn). ...

  5. 原生app,WEBAPP,混合app

    什么叫做原生App? 原生App是专门针对某一类移动设备而生的,它们都是被直接安装到设备里,而用户一般也是通过网络商店或者卖场来获取例如    The App Store  与  Android Ap ...

  6. libevent入门(1)

    libevent是什么                libevent 库实际上没有更换 select().poll() 或其他机制的基础.而是使用对于每个平台最高效的高性能解决方案在实现外加上一个包 ...

  7. Openjudge 百练第4109题

    在OpenJudge看到一个题目(#4109),题目描述如下: 小明和小红去参加party.会场中总共有n个人,这些人中有的是朋友关系,有的则相互不认识.朋友关系是相互的,即如果A是B的朋友,那么B也 ...

  8. iOS 的 APP 在系统中如何适配不同的屏幕的尺寸

    iOS 的 APP 在系统中如何适配不同的屏幕的尺寸 标签: 2007年,初代iPhone发布,屏幕的宽高是 320 x 480 像素.下文也是按照宽度,高度的顺序排列.这个分辨率一直到iPhone ...

  9. SDWebImage 官方文档

    API documentation is available at CocoaDocs - SDWebImage Using UIImageView+WebCache category with UI ...

  10. TVS_压敏电阻等保护类器件的布局问题

    世上本没有垃圾,垃圾都是放错了位置的资源. 对于电路来说,保护器件就是其保护作用的关键资源,但如果放错了位置,它就是垃圾:甚至不仅是垃圾,而还可能成为祸害. 由最近承接的几起电路原理图审核项目来看,触 ...