Skip to content
This repository was archived by the owner on Mar 20, 2024. It is now read-only.

Commit 9f0ff2e

Browse files
Foxandxsswardbell
authored andcommitted
docs(attribute-directives): add Renderer to style elements
closes #2164
1 parent d01898f commit 9f0ff2e

4 files changed

Lines changed: 12 additions & 15 deletions

File tree

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* tslint:disable:no-unused-variable */
22
// #docregion
3-
import { Directive, ElementRef, Input } from '@angular/core';
3+
import { Directive, ElementRef, Input, Renderer } from '@angular/core';
44

55
@Directive({ selector: '[myHighlight]' })
66
export class HighlightDirective {
7-
constructor(el: ElementRef) {
8-
el.nativeElement.style.backgroundColor = 'yellow';
7+
constructor(el: ElementRef, renderer: Renderer) {
8+
renderer.setElementStyle(el.nativeElement, 'backgroundColor', 'yellow');
99
}
1010
}

public/docs/_examples/attribute-directives/ts/app/highlight.directive.2.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
/* tslint:disable:no-unused-variable */
22
// #docregion
3-
import { Directive, ElementRef, HostListener, Input } from '@angular/core';
3+
import { Directive, ElementRef, HostListener, Input, Renderer } from '@angular/core';
44

55
@Directive({
66
selector: '[myHighlight]'
77
})
88

99
export class HighlightDirective {
1010
// #docregion ctor
11-
private el: HTMLElement;
12-
13-
constructor(el: ElementRef) { this.el = el.nativeElement; }
11+
constructor(private el: ElementRef, private renderer: Renderer) { }
1412
// #enddocregion ctor
1513

1614
// #docregion mouse-methods, host
@@ -28,7 +26,7 @@ export class HighlightDirective {
2826
// #enddocregion host
2927

3028
private highlight(color: string) {
31-
this.el.style.backgroundColor = color;
29+
this.renderer.setElementStyle(this.el.nativeElement, 'backgroundColor', color);
3230
}
3331
// #enddocregion mouse-methods
3432

public/docs/_examples/attribute-directives/ts/app/highlight.directive.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
// #docplaster
22
// #docregion full
3-
import { Directive, ElementRef, HostListener, Input } from '@angular/core';
3+
import { Directive, ElementRef, HostListener, Input, Renderer } from '@angular/core';
44

55
@Directive({
66
selector: '[myHighlight]'
77
})
88
// #docregion class
99
export class HighlightDirective {
1010
private _defaultColor = 'red';
11-
private el: HTMLElement;
1211

13-
constructor(el: ElementRef) { this.el = el.nativeElement; }
12+
constructor(private el: ElementRef, private renderer: Renderer) { }
1413
// #enddocregion class
1514

1615
// #docregion defaultColor
@@ -34,7 +33,7 @@ export class HighlightDirective {
3433
}
3534

3635
private highlight(color: string) {
37-
this.el.style.backgroundColor = color;
36+
this.renderer.setElementStyle(this.el.nativeElement, 'backgroundColor', color);
3837
}
3938
}
4039
// #enddocregion class

public/docs/ts/latest/guide/attribute-directives.jade

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ block highlight-directive-1
7171
We need the `Directive` symbol for the `@Directive` decorator.
7272
We need the `ElementRef` to [inject](dependency-injection.html) into the directive's constructor
7373
so we can access the DOM element.
74+
We also need `Renderer` so we can change the DOM element's style.
7475
We don't need `Input` immediately but we will need it later in the chapter.
7576

7677
Then we define the directive metadata in a configuration object passed
@@ -101,11 +102,10 @@ p
101102
| We export `HighlightDirective` to make it accessible to other components.
102103
:marked
103104
Angular creates a new instance of the directive's controller class for
104-
each matching element, injecting an Angular `ElementRef`
105+
each matching element, injecting an Angular `ElementRef` and `Renderer`
105106
into the constructor.
106107
`ElementRef` is a service that grants us direct access to the DOM element
107-
through its `nativeElement` property.
108-
That's all we need to set the element's background color using the browser DOM API.
108+
through its `nativeElement` property and with `Renderer` we can set the element style.
109109

110110
.l-main-section
111111
a#apply-directive

0 commit comments

Comments
 (0)