docs(attribute-directives): add Renderer to style elements

closes #2164
This commit is contained in:
Jesús Rodríguez 2016-08-22 11:35:22 +02:00 committed by Ward Bell
parent d01898f474
commit 9f0ff2e2a9
4 changed files with 12 additions and 15 deletions

View File

@ -1,10 +1,10 @@
/* tslint:disable:no-unused-variable */ /* tslint:disable:no-unused-variable */
// #docregion // #docregion
import { Directive, ElementRef, Input } from '@angular/core'; import { Directive, ElementRef, Input, Renderer } from '@angular/core';
@Directive({ selector: '[myHighlight]' }) @Directive({ selector: '[myHighlight]' })
export class HighlightDirective { export class HighlightDirective {
constructor(el: ElementRef) { constructor(el: ElementRef, renderer: Renderer) {
el.nativeElement.style.backgroundColor = 'yellow'; renderer.setElementStyle(el.nativeElement, 'backgroundColor', 'yellow');
} }
} }

View File

@ -1,6 +1,6 @@
/* tslint:disable:no-unused-variable */ /* tslint:disable:no-unused-variable */
// #docregion // #docregion
import { Directive, ElementRef, HostListener, Input } from '@angular/core'; import { Directive, ElementRef, HostListener, Input, Renderer } from '@angular/core';
@Directive({ @Directive({
selector: '[myHighlight]' selector: '[myHighlight]'
@ -8,9 +8,7 @@ import { Directive, ElementRef, HostListener, Input } from '@angular/core';
export class HighlightDirective { export class HighlightDirective {
// #docregion ctor // #docregion ctor
private el: HTMLElement; constructor(private el: ElementRef, private renderer: Renderer) { }
constructor(el: ElementRef) { this.el = el.nativeElement; }
// #enddocregion ctor // #enddocregion ctor
// #docregion mouse-methods, host // #docregion mouse-methods, host
@ -28,7 +26,7 @@ export class HighlightDirective {
// #enddocregion host // #enddocregion host
private highlight(color: string) { private highlight(color: string) {
this.el.style.backgroundColor = color; this.renderer.setElementStyle(this.el.nativeElement, 'backgroundColor', color);
} }
// #enddocregion mouse-methods // #enddocregion mouse-methods

View File

@ -1,6 +1,6 @@
// #docplaster // #docplaster
// #docregion full // #docregion full
import { Directive, ElementRef, HostListener, Input } from '@angular/core'; import { Directive, ElementRef, HostListener, Input, Renderer } from '@angular/core';
@Directive({ @Directive({
selector: '[myHighlight]' selector: '[myHighlight]'
@ -8,9 +8,8 @@ import { Directive, ElementRef, HostListener, Input } from '@angular/core';
// #docregion class // #docregion class
export class HighlightDirective { export class HighlightDirective {
private _defaultColor = 'red'; private _defaultColor = 'red';
private el: HTMLElement;
constructor(el: ElementRef) { this.el = el.nativeElement; } constructor(private el: ElementRef, private renderer: Renderer) { }
// #enddocregion class // #enddocregion class
// #docregion defaultColor // #docregion defaultColor
@ -34,7 +33,7 @@ export class HighlightDirective {
} }
private highlight(color: string) { private highlight(color: string) {
this.el.style.backgroundColor = color; this.renderer.setElementStyle(this.el.nativeElement, 'backgroundColor', color);
} }
} }
// #enddocregion class // #enddocregion class

View File

@ -71,6 +71,7 @@ block highlight-directive-1
We need the `Directive` symbol for the `@Directive` decorator. We need the `Directive` symbol for the `@Directive` decorator.
We need the `ElementRef` to [inject](dependency-injection.html) into the directive's constructor We need the `ElementRef` to [inject](dependency-injection.html) into the directive's constructor
so we can access the DOM element. so we can access the DOM element.
We also need `Renderer` so we can change the DOM element's style.
We don't need `Input` immediately but we will need it later in the chapter. We don't need `Input` immediately but we will need it later in the chapter.
Then we define the directive metadata in a configuration object passed Then we define the directive metadata in a configuration object passed
@ -101,11 +102,10 @@ p
| We export `HighlightDirective` to make it accessible to other components. | We export `HighlightDirective` to make it accessible to other components.
:marked :marked
Angular creates a new instance of the directive's controller class for Angular creates a new instance of the directive's controller class for
each matching element, injecting an Angular `ElementRef` each matching element, injecting an Angular `ElementRef` and `Renderer`
into the constructor. into the constructor.
`ElementRef` is a service that grants us direct access to the DOM element `ElementRef` is a service that grants us direct access to the DOM element
through its `nativeElement` property. through its `nativeElement` property and with `Renderer` we can set the element style.
That's all we need to set the element's background color using the browser DOM API.
.l-main-section .l-main-section
a#apply-directive a#apply-directive