[Angular Directive] Combine HostBinding with Services in Angular 2 Directives
2016-12-22 17:07
507 查看
You can change behaviors of element and
@Componentproperties based on services using
@HostBindingin
@Directives. This allows you to build
@Directives which rely on services to change behavior without the
@Componentever 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; } }
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; } }
<button online>One</button>
相关文章推荐
- [Angular Directive] Implement Structural Directive Data Binding with Context in Angular
- [AngularJS] Using Services in Angular Directives
- [Angular2 Form] Group Inputs in Angular 2 Forms with ngModelGroup
- Sending files in chunks with MTOM Web Services and .NET 2.0
- Host Python In your Application with IronPython2.0.1 (一): 创建交互环境
- Create web application with Host Header in SharePoint 2013
- Working with RESTful Services in CodeIgniter
- Working with collections in WCF RIA Services (part 1)
- [Angular] Use Angular’s @HostBinding and :host(...) to add styling to the component itself
- [Angular Unit Testing] Testing Services with dependencies
- Unable to resolve host "stats.umsns.com": No address associated with hostname: [Caught Exception in
- Host Python in your application with IronPython 2.0.1(二): 互相访问
- 好文推荐:Development Tools and Techniques for Working with Code in Windows SharePoint Services 3.0
- Working with collections in WCF RIA Services (part 2)
- Hosting WCF Service with netTcpBinding in Windows Service
- Using An Item Template With An HTML Dropdown Menu Component In Angular 2 RC 3
- Angular @HostBinding()和@HostListener()用法
- 好文推荐:Development Tools and Techniques for Working with Code in Windows SharePoint Services 3.0
- angular 事件绑定/属性绑定 @HostListener ,@HostBinding
- Email Services in Salesforce with simple example