[Angular 2] Using the @Inject decorator
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的更多相关文章
- [Angular 2] Value Providers & @Inject
Dependecies aren’t always objects created by classes or factory functions. Sometimes, all we really ...
- 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 ...
- [Angular 2] Inject Service
TypeScript is used heavily as we build up our application, but TypeScript isn’t required. If you wan ...
- Angular源代码学习笔记-原创
时间:2014年12月15日 14:15:10 /** * @license AngularJS v1.3.0-beta.15 * (c) 2010-2014 Google, Inc. http:// ...
- Angular 4+ 修仙之路
Angular 4.x 快速入门 Angular 4 快速入门 涉及 Angular 简介.环境搭建.插件表达式.自定义组件.表单模块.Http 模块等 Angular 4 基础教程 涉及 Angul ...
- Angular指令1
Angular的指令 也就是directive,其实就是一WebComponent,以前端的眼光来看,好象很复杂,但是以后端的眼光来看,还是非常简单的.其实就是一个中等水平的类. var myModu ...
- 【17】进大厂必须掌握的面试题-50个Angular面试
我们整理了一份主要的Angular面试问题清单,分为三部分: 角度面试问题–初学者水平 角度面试问题–中级 角度面试问题–高级 初学者水平–面试问题 1.区分Angular和AngularJS. 特征 ...
- Angular SSR 探究
一般来说,普通的 Angular 应用是在 浏览器 中运行,在 DOM 中对页面进行渲染,并与用户进行交互.而 Angular Universal 是在 服务端 进行渲染(Server-Side Re ...
- Angularjs 源码
/** * @license AngularJS v1.3.0-beta.15 * (c) 2010-2014 Google, Inc. http://angularjs.org function t ...
随机推荐
- JavaScript异步编程 ( 一 )
1. 异步编程 Javascript语言的执行环境是"单线程"(single thread).所谓"单线程",就是指一次只能完成一件任务.如果有多个任务,就必须 ...
- jQuery简介以及jQuery选择器
一 简介 1 定义:jQuery库是JavaScript的封装库 2 优点: 1) : 代码开源 2) : 选择器强大 3) : 完善的Ajax 4) : 浏览器兼容性高 5) : 文档完善(帮助文档 ...
- EF中使用SqlQuery进行参数化查询时抛出异常
EF中使用带参数的SqlQuery进行查询时,抛出"另一个 sqlparametercollection 中已包含 sqlparameter"异常. 这是由于SqlParamert ...
- ORACLE中将数字转换为英文
SELECT LEVEL, to_char(to_date(LEVEL,'J'),'Jsp') FROM dual CONNECT 运行结果如下图所示: 说明: TO_CHAR(aDate,'JS ...
- asp.net 发送邮件代码 System.Net.Mail
前台页面 SendEmail.aspx 代码 using System.Net.Mail;using System.Net; <h2> 发送电子邮件演示 </h2> <t ...
- C#结构函数与base关键字
//声明父类 class ProductsFather { public double Price { get; set; } public int Count { get; set; } publi ...
- 自己手动绿色化MyEclipse
绿化过程因每个人的文件存放路径不同而不同 首先打开你解压的MyEclipse文件,或者以前安装的MyEclipse重装系统后不能用,打开到这里:记住路径,比如我的是:D:\MyEclipse 我们打开 ...
- poj3254状压DP入门
G - 状压dp Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:65536KB 64bit ...
- css3中的提供的元素变化属性
通过 CSS3 提供的2d元素转换,我们能够对元素进行移动.缩放.转动.拉长或拉伸. css3中为我们 提供了: translate() rotate() scale() skew() matrix( ...
- phpmyadmin自增字段
自增字段必须为primary key 2种方法: 1- ALTER TABLE `qr_role` CHANGE `ROLE_ID` `ROLE_ID` INT(11) NOT NULL AUTO_I ...