You can change behaviors of element and @Component properties based on services using @HostBinding in @Directives. This allows you to build @Directives 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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. angular directive scope

    angular directive scope 1.当directive 中不指定scope属性,则该directive 直接使用 app 的scope: 2.当directive 中指定scope属 ...

  6. angular directive自定义指令

    先来看一下自定义指令的写法 app.directive('', ['', function(){ // Runs during compile return { // name: '', // pri ...

  7. angular directive 深入理解

    由于业务的需要,最近angular 的diretive 研究的比较多,有和同事一起共同协作开发scada的项目, 对directive 有了进一步更深的理解. 感觉才开始真正理解了这句话的意思: In ...

  8. [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 ...

  9. 从Java角度理解Angular之入门篇:npm, yarn, Angular CLI

    本系列从Java程序员的角度,带大家理解前端Angular框架. 本文重点介绍Angular的开发.编译工具:npm, yarn, Angular CLI,它们就像Java在中的Maven,同时顺便介 ...

随机推荐

  1. javascript创建对象的方法--动态原型模式

    javascript创建对象的方法--动态原型模式 一.总结 1.作用:解决组合模式的属性和函数分离问题  2.思路:基本思路和组合模式相同:共用的函数和属性用原型方式,非共用的的函数和属性用构造函数 ...

  2. vue使用marked.js实现markdown转html并提取标题生成目录

    html: <template> <div class="wrapper"> <div class="container"> ...

  3. 洛谷 P4779【模板】单源最短路径(标准版)

    洛谷 P4779[模板]单源最短路径(标准版) 题目背景 2018 年 7 月 19 日,某位同学在 NOI Day 1 T1 归程 一题里非常熟练地使用了一个广为人知的算法求最短路. 然后呢? 10 ...

  4. 15.lambda表达式

    #include <iostream> #include <array> using namespace std; //解决函数怀孕现象 //[](){} //[] =引用,只 ...

  5. 使用ILMerge将所有引用的DLL和exe文件打成一个exe文件

    今天做了一个IM自动更新的软件,里面牵扯到了文件的解压和接口签名加密,使用了2个第三方的dll,想发布的时候才发现调用的类没几个,就像把它们都跟EXE文件打包在一起,以后复制去别的地方用也方便,于是上 ...

  6. Python 极简教程(十一)字典 dict

    字典是以大括号标识,以键值对(key:value)的形式,无序,不可重复,可变的集合类型. 字典具有非常高效的读写效率. >>> d = {} # 创建一个空字典 >>& ...

  7. leetcode笔记:Word Break

    一. 题目描写叙述 Given a string s and a dictionary of words dict, determine if s can be segmented into a sp ...

  8. Cscope how to support java and c++

    Cscope 首先在文件夹下建立cscope索引文件 find -name '*.c' > cscope.file cscope -Rbkq 这个命令会生成三个文件:cscope.out, cs ...

  9. 《你不知道的JavaScript(上)》笔记——提升

    笔记摘自:<你不知道的JavaScript(上)>第3章 提升 1.包括变量和函数在内的所有声明都会在任何代码被执行前首先被处理. 2.变量和函数声明从它们在代码中出现的位置被“移动”到了 ...

  10. swiper如何实现轮播嵌套轮播

    之所以要写这篇文章是因为插件有个bug,要改掉这个bug比较麻烦,所以就想了个折中的办法,绕过这个限制,方法千万条,功能干出来第一条,哈哈 最近做了个需求,效果图是这样的 第一个框是大轮播,第二个框是 ...