Use for questions about Angular 2+ directives
Questions tagged [angular2-directives]
1806 questions
68
votes
2 answers
angular 4: *ngIf with multiple conditions
I'm confused a bit. I need to hide block if result have one of several cases. But seems it not working correctly...

Merge-pony
- 1,668
- 3
- 18
- 32
67
votes
8 answers
Angular: Cannot find a differ supporting object '[object Object]'
Im following this tutorial. On the way to get list of users from api.github Im getting error:
Cannot find a differ supporting object '[object Object]'
I think its related to
- {{user | json}} …

blackHawk
- 6,047
- 13
- 57
- 100
62
votes
5 answers
Detect when input value changed in directive
I'm trying to detect when the value of an input changed in a directive. I have the following directive:
import { ElementRef, Directive, Renderer} from '@angular/core';
@Directive({
selector: '[number]',
host: {"(input)":…

Nate
- 7,606
- 23
- 72
- 124
61
votes
2 answers
How to dynamically add a directive?
How to dynamically add (inject) a directive into host?
I have a myTooltip directive and I would like to add mdTooltip directive to it's host. I have tried setAttribute() of ElementRef.nativeElement, but it doesn't create the mdTooltip…

RichieRock
- 1,138
- 1
- 8
- 13
60
votes
3 answers
How to put a component inside another component in Angular2?
I'm new at Angular and I'm still trying to understand it. I've followed the course on the Microsoft Virtual Academy and it was great, but I found a little discrepancy between what they said and how my code behave! Specifically, I've tried to "put a…

Paolo Ardissone
- 837
- 1
- 6
- 15
59
votes
4 answers
How to import component into another root component in Angular 2
I am trying to import component from one file another root component file.
it give error as ..
zone.js:484 Unhandled Promise rejection: Template parse errors:
'courses' is not a known element:
1. If 'courses' is an Angular component, then…

Vishu
- 1,105
- 3
- 10
- 19
59
votes
6 answers
Angular2 Styles in a Directive
In the given examples of Attribute directives (i.e. a directive to add appearance/behaviour), we have a fairly simple setting of a style on the host element.. e.g.
import {Directive, ElementRef } from 'angular2/core';
@Directive({
selector:…

ct5845
- 1,428
- 3
- 16
- 20
59
votes
7 answers
Implementing autocomplete
I am having trouble finding a good autocomplete component for Angular2. Just anything that I can pass a list of key-label objects to and have a nice autocomplete on an input field.
Kendo does not support Angular 2 yet and that it what we mostly use…

Mort
- 837
- 1
- 14
- 20
57
votes
4 answers
Using a directive to add class to host element
I am currently learning Angular 2. I understood how to use the Angular Renderer to set an ElementStyle, but now I would like to use the Renderer method:
setElementClass(renderElement: any, className: string, isAdd: boolean) : void
My question is…

Daniel Hoppe Alvarez
- 1,298
- 1
- 13
- 18
56
votes
2 answers
How to declare a directive globally for all modules?
I'm developing a Github repo which follows the offical tutorial of Angular (Tour of Heroes). You can see all the code here.
My problem, is that I have a directive declared in the main module of the app (app.module) and, if I use it inside the…

ismaestro
- 7,561
- 8
- 37
- 50
53
votes
5 answers
angular 2 access ng-content within component
How can I access the "content" of a component from within the component class itself?
I would like to do something like this:
my text to transform to upper case
How can I get the content or the upper tag within my component like I…

Daniel Kobler
- 533
- 1
- 4
- 5
52
votes
5 answers
How to detect scroll to bottom of html element
I have this element that I'm referencing by Id:
let infiniteScrollElement = document.getElementById('th-infinite-scroll-tracker');
I need to listen when the browser is has reached the bottom of the element.
How to achieve this?

CommonSenseCode
- 23,522
- 33
- 131
- 186
50
votes
6 answers
Dynamic routerLink value from ngFor item giving error "Got interpolation ({{}}) where expression was expected"
I'm trying to set the routerLink value in a directive based on a dynamic set of items from the component. But an error is being thrown from Angular2:
EXCEPTION: Template parse errors:
Parser Error: Got interpolation ({{}}) where expression was…

BenR
- 2,791
- 3
- 26
- 36
49
votes
2 answers
Angular2 ngFor skip first index
How can I skip the first index from the array?
{{ user.name }} is {{ user.age }} years old.

Basit
- 16,316
- 31
- 93
- 154
48
votes
4 answers
*ngIf and *ngFor on element
I have a situation where I need *ngIf and *ngFor directive on the same element. I found lot of answers on the stackoverlow but none for the this type of situation.
I have a table in which I'm looping through the array of objects and dynamically…

Igor Janković
- 5,494
- 6
- 32
- 46