[Angular Directive] Combine HostBinding with Services in Angular 2 Directives
You can change behaviors of element and @Component
properties based on services using @HostBinding
in @Directives
. This allows you to build @Directive
s which rely on services to change behavior without the @Component
ever needing to know that the Service even exists.
import {Directive, HostBinding} from '@angular/core';
import {OnlineService} from "../services/online.service"; @Directive({
selector: '[online]'
})
export class OnlineDirective { constructor(private onlineService: OnlineService) { } @HostBinding('style.color') get styleColor () {
return !this.onlineService.online ? 'red': 'unset';
}
@HostBinding('disabled') get disabled() {
return !this.onlineService.online;
}
}
@Injectable()
export class OnlineService{
online = true
constructor(){
setInterval(()=>{
this.online = Math.random() > .5
}, 1000)
}
}
<button online>One</button>
[Angular Directive] Combine HostBinding with Services in Angular 2 Directives的更多相关文章
- [Angular Directive] Build a Directive that Tracks User Events in a Service in Angular 2
A @Directive is used to add behavior to elements and components in your application. This makes @Dir ...
- [Angular Directive] 3. Handle Events with Angular 2 Directives
A @Directive can also listen to events on their host element using @HostListener. This allows you to ...
- [Angular Directive] 2. Add Inputs to Angular 2 Directives
The @Input decorator allows you to pass values into your @Directive so that you can change the value ...
- [Angular Directive] Create a Template Storage Service in Angular 2
You need to define a <template> to be able to use it elsewhere in your app as a TemplateRef. Y ...
- angular directive scope
angular directive scope 1.当directive 中不指定scope属性,则该directive 直接使用 app 的scope: 2.当directive 中指定scope属 ...
- angular directive自定义指令
先来看一下自定义指令的写法 app.directive('', ['', function(){ // Runs during compile return { // name: '', // pri ...
- angular directive 深入理解
由于业务的需要,最近angular 的diretive 研究的比较多,有和同事一起共同协作开发scada的项目, 对directive 有了进一步更深的理解. 感觉才开始真正理解了这句话的意思: In ...
- [Angular Directive] Write a Structural Directive in Angular 2
Structural directives enable you to use an element as a template for creating additional elements. C ...
- 从Java角度理解Angular之入门篇:npm, yarn, Angular CLI
本系列从Java程序员的角度,带大家理解前端Angular框架. 本文重点介绍Angular的开发.编译工具:npm, yarn, Angular CLI,它们就像Java在中的Maven,同时顺便介 ...
随机推荐
- IAR FOR STM8 学习笔记 IAR工程的建立
STM8是ST意法半导体针对工业应用和消费电子开发而推出的8位单片机. 每种MCU都有自身的优点与缺点,与其它8-bit MCU相比,STM8 8-bit MCU最大的特点是: · 内核: o 最高f ...
- asp.net 前后台数据交互方式(转)
https://blog.csdn.net/luckyrass/article/details/38758007 一.前台直接输出后台传递的数据 后台代码: // .aspx.cs public st ...
- Linux文本编辑器
1.编辑模式 2.命令模式 3.底部命令模式 注意:如果发现编辑不了.可能是因为非法退出产生一个后缀名为.swp 的临时隐藏文件. 将其删除重新编辑即可!
- 3/18 Django框架 启动django服务
web框架:本质是socket服务端,socket通常也被称为"套接字",用于描述IP地址和端口,是一个通信链的句柄,可以用来实现不同虚拟机或不同计算机之间的通信.web框架就是将 ...
- CISP/CISA 每日一题 15
CISA 每日一题(答) 作业记帐: 监控和记录信息系统资源的使用,这些信息可被信息系统审计师用来执行: 1.将资源使用和相关用户挂钩以便实行计费: 2.通过改变系统软件的默认设置来最优化硬件性能 作 ...
- POJ 3592--Instantaneous Transference【SCC缩点新建图 && SPFA求最长路 && 经典】
Instantaneous Transference Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6177 Accep ...
- C#操作session的类实例
本文实例讲述了C#操作session的类.分享给大家供大家参考.具体分析如下: 这个C#类对session操作进行了再次封装,可以大大简化session的常用操作,同时这个类可以将session值设置 ...
- python关于字典的操作
https://www.cnblogs.com/RENQIWEI1995/p/7931374.html 最常用的代码举例: dict = {'Name': 'Zara', 'Age': 7, 'Cla ...
- UVA - 590Always on the run(递推)
题目:UVA - 590Always on the run(递推) 题目大意:有一个小偷如今在计划着逃跑的路线,可是又想省机票费. 他刚開始在城市1,必须K天都在这N个城市里跑来跑去.最后一天达到城市 ...
- Qt产生随机数(两种方法)
第一种方法 #include <QTime> QTime time; time= QTime::currentTime(); qsrand(time.msec()+time.second( ...