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. Android摇一摇振动效果Demo

    前言     在微信刚流行的时候,在摇一摇还能用来那啥的时候,我也曾深更半夜的拿着手机晃一晃.当时想的最多的就是.我靠,为神马摇一下须要用这么大的力度,当时我想可能腾讯认为那是个人性的设计.后来才发觉 ...

  2. C#去除byte数组头尾杂质(即不需要的数据)

    代码如下: /// <summary> /// 去除byte数组头尾杂质(即不需要的数据) /// </summary> /// <param name="ar ...

  3. Nginx常见502错误

    1.配置错误因为nginx找不到php-fpm了,所以报错,一般是fastcgi_pass后面的路径配置错误了,后面可以是socket或者是ip:port2.资源耗尽lnmp架构在处理php时,ngi ...

  4. css变形几大属性

    1.transform: transform-function() * | none; transform-function: translate().scale().rotate().skew(). ...

  5. 网页调用QQ聊天

    把下面的复制到地址栏里,QQ号为你要聊天的人的qq号,如果你没有登录你自己的qq,首先会调出qq登录窗体. tencent://message/?uin=QQ号­

  6. HttpWebRequest多线程抓取17Track的物流信息

    公司的一个系统需要去抓17Track的物流信息,贴上代码有需要的朋友可以参考一下↓ //17Track的抓取地址以及开启的线程数量 <add key="url" value= ...

  7. (转)js学习笔记()函数

    1.调用函数时,如果参数多于定义时的个数,则多余的参数将会被忽略,如果少于定义时的个数则缺失的参数数会被自动赋予undefined值. 2.如果是用function语句声明的函数定义则不可以出现在循环 ...

  8. 服务器上开启远程sqlserver小细节

    之前按网络上过程配置完成后,连接都很正常.今天看的时候,突然就连接不上了.弄了半天,终于知道问题在哪里了.

  9. 统计"面"要素中"点"要素的个数.

    步骤 1,创建字段 IFields /// <summary> /// 创建:"面"-"点数"的字段. /// </summary> / ...

  10. QueryFilter与SpatialFilter - 浅谈

    我们知道,GIS不仅仅有属性查询,还有空间查询.而 QueryFilter 对应于 属性查询,而 SpatialFilter 对应于 空间查询.