01 Part2 CustomDirectives FilterPipe
01 Part2 CustomDirectives FilterPipe
1
Directives Recap
Does not change the DOM, only the Changes DOM directly by adding or
element to which it is added removing elements
2
Assignment recap directives
3
Custom Directives
ng g d
• Create:
[NameDirective]
• Usage:
in an html template
<p appNameDirective>blah,
put the selector blah,
in the html element you want to use it on.
blah</p>
4
Custom Directives (example)
app.component.html highlight.directive.ts
(can also be used in other HTML templates)
app.component.ts
Html rendering:
5
Dependency Injection (recap)
7
Custom Directives (example with renderer)
highlight2.directive.ts Check the angular
documentation for all
available functions:
https://angular.io/api/core/Renderer2
app.component.html
Html rendering:
8
Style element dynamically (HostListener)
markeer3.directive.ts
highlight3.directive.ts In this example the
HostListener is used to
listen for events
(mouseenter, mouseleave)
on the host element. This is
the element the directive is
placed on
Html rendering:
app.component.html
9
HostBinding instead of renderer
markeer3.directive.ts
highlight4.directive.ts Via host binding, you connect
the property of the host
element (between the
brackets) to the directive
property (here
myBackGroundColor)
You can bind with all
properties of the host element
Html rendering:
app.component.html
10
Directives with @Input
@Input can be used to pass values to the directive.
Note: input value is not know in constructor, you
highlight5.directive.ts need the onInit lifecycle hook for that one!
app.component.html
Html rendering:
11
Directives with @Input and alias
If @Input is given an alias which is the same as the
highlight6.directive.ts
name of the directive, it can also be done in the
following way.
This is exactly the same as what we do with ngStyle
and ngClass
app.component.html
Html rendering:
12
Assignment 2
13
Structural Directives
14
Create a custom Structural Directive
my-else.directive.ts
app.component.html
15
Create a custom Structural Directive
16
Assignment 3
17
Filter pipe
typeScript => list of applications that are shown in HTML template with *ngFor loop
18
Filter pipe Bind value in input field to propertyfilteredStatus
via 2-way binding
property filteredStatus
- the property to filter (here status)
19
Filter pipe
True (default): filtered output is updated only when extra filterinput changes
=> changes in HTML (in this case add an app) not visible as long
as filterString does not change
false: filtered output updates at every change (also without changing filterString)
=> more resource intensive but sometimes necessary!
Array
Arraytotowhich
whichyouyouapply
applythis
thisfilter
filterisisempty
emptyor
or
filterString
filterStringisisempty
empty=>
=>leave
leavearray
arrayas asisis
Create
Createnew
newarray
arraywith
withfiltered
filteredresult
result
20
Assignment 4
21