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. 2.vector数组

    创建vector数组 vector<,,,, }; 尾部插入 ; i < ; i++) { myint.push_back(i); } 读取头部和尾部 cout << myin ...

  2. Express简介、安装

    Express 基于Node.js平台,快速.开放.极简的web开发框架,是目前最流行的基于Node.js的web开发框架,它提供一系列强大的功能,比如: 路由控制 参数获取 send和sendFil ...

  3. 洛谷P3613 睡觉困难综合征(LCT)

    题目: P3613 睡觉困难综合症 解题思路: LCT,主要是维护链上的多位贪心答案,推个公式:分类讨论入0/1的情况,合并就好了(公式是合并用的) 代码(我不知道之前那个为啥一直wa,改成结构体就好 ...

  4. 《机器学习实战》基于朴素贝叶斯分类算法构建文本分类器的Python实现

    ============================================================================================ <机器学 ...

  5. phoenixframe自己主动化平台在Linux环境下运行用例的说明

    phoenixframe自己主动化平台支持在Linux环境下使用phantomjs,Firefox.chrome运行測试用例.但有下面几个问题须要注意: 1.若无法启动phantomjs,Firefo ...

  6. 网页设计实战3 ufo类型的科技网页如何实现

    网页设计实战3 ufo类型的科技网页如何实现 一.总结 一句话总结:基础的图片素材就是如何几张图片,这个效果只是通过jquery或者js让那个png图片旋转起来了,如此而已.其实核心就是一个trans ...

  7. Xamarin开发手机聊天程序

    使用Xamarin开发手机聊天程序 -- 基础篇(大量图文讲解 step by step,附源码下载)   如果是.NET开发人员,想学习手机应用开发(Android和iOS),Xamarin 无疑是 ...

  8. 10.Maven依赖排除 禁止依赖传递 取消依赖的方法

    转自:https://www.cnblogs.com/duanxz/p/6084494.html 大家都知道Maven的优点是依赖管理,特别是前期使用ANT的开发者都有很多感触.最近要开发一个java ...

  9. mysql中配置ssl_key、ssl-cert、ssl-ca的路径及建立ssl连接(适用于5.7以下版本,5.7及以上请看本文末尾的备注)

    1.创建 CA 私钥和 CA 证书 (1)下载并安装openssl,将bin目录配置到环境变量: (2)设置openssl.cfg路径(若不设置会报错,找不到openssl配置文件) \bin\ope ...

  10. JQuery 当点击input后,单选多选的选中状态

    1.当点击input元素,此元素的选中checked的值 = 此元素此时表现的选中与否的状态. eg:input元素开始是未选中,点击后表现的是选中状态,此元素的checked为true(和此元素开始 ...