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. Linux驱动开发常用头文件

    头文件目录中总共有32个.h头文件.其中主目录下有13个,asm子目录中有4个,linux子目录中有10个,sys子目录中有5个.这些头文件各自的功能如下: 1.主目录 <a.out.h> ...

  2. 内容提供者 ContentResolver 数据库 示例 -2

    MainActivity public class MainActivity extends ListActivity {     // 访问内容提供者时需要的主机名称     public stat ...

  3. Accordion( 分类) 组件

    一. 加载方式 //class 加载方式<div id="box" class="easyui-accordion"style="width:3 ...

  4. js判断值是否为数字

    js判断是否是数字 第一种方法 isNaN isNaN 返回一个 Boolean 值,指明提供的值是否是保留值 NaN (不是数字). NaN 即 Not a Number isNaN(numValu ...

  5. Swift--基础(一)基本类型 符号 字符串(不熟的地方)

    常量 变量 let age = 20 常量不可变 var num = 24 变量可变 let count:Int = 2 定义类型 Double(count)  类型转换 符号 1.?? let de ...

  6. angular ng-bind-html 对src路径失效 解决方案

    json内容 ;<img src="/newsfile/1506271512489.jpg" width="600" height="450&q ...

  7. HTML5 布局标签

    HTML5是HTML标准的下一个版本.越来越多的程序员开始HTML5来构建网站,相对HTML4,HTML5新增的带有语义化的标签可以代替div进行页面布局(与html5.js结合起来时可以放心使用 ) ...

  8. 转载:mysql-Auto_increment值修改

    转载网址:http://libo93122.blog.163.com/blog/static/1221893820125282158745/ | 2012-03-13 11:19:10 | 2012- ...

  9. div 居中进行总结

    1.margin:auto ;让元素居中,需要确定元素的宽度,并且需要是块元素 eg: div { width:200px; height:200px; background:#222; margin ...

  10. Android MVP模式 简单易懂的介绍方式

    主要学习这位大神的博客:简而易懂 Android MVP模式 简单易懂的介绍方式 https://segmentfault.com/a/1190000003927200