How to detect scroll to bottom of html element

You could check whether the user has scrolled to the bottom or not in the below way… Html file <div (scroll)=”onScroll($event)”> … … </div> typescript file onScroll(event: any) { // visible height + pixel scrolled >= total height if (event.target.offsetHeight + event.target.scrollTop >= event.target.scrollHeight) { console.log(“End”); } }

angular 2 access ng-content within component

If you want to get a reference to a component of the transcluded content, you can use: @Component({ selector: ‘upper’, template: `<ng-content></ng-content>` }) export class UpperComponent { @ContentChild(SomeComponent) content: SomeComponent; } If you wrap <ng-content> then you can access access to the transcluded content like @Component({ selector: ‘upper’, template: ` <div #contentWrapper> <ng-content></ng-content> </div>` }) … Read more

How to declare a directive globally for all modules?

According @CrazyMac’s comment, a good way would be to create a DirectivesModule. Inside this module you can declare and import all your directives then you will be able to import this module anywhere you want. app/modules/directives/directives.module.ts import { NgModule } from ‘@angular/core’; import { HighlightDirective } from ‘./highlight.directive’; @NgModule({ imports: [], declarations: [HighlightDirective], exports: [HighlightDirective] … Read more

Using a directive to add class to host element [duplicate]

Original OP was asking how to use Renderer. I’ve included the @HostBinding for completeness. Using @HostBinding To add a class to an element you can use @HostBinding import { Directive, HostBinding} from ‘@angular/core’; @Directive({ selector: ‘[myDirective]’, }) export class MyDirective { @HostBinding(‘class’) elementClass=”custom-theme”; constructor() { } } Using @HostBinding with multiple classes To make multiple … Read more

How to dynamically add a directive?

That is a feature we are asking for in angular…read this: https://github.com/angular/angular/issues/8785 A quick and dirty way to do it is to use: I have a directive named myHilite (to highlight text), I also have a component named MainComponent.ts. In MainComponent.ts I added this line of code… export class MainComponent { @HostBinding(‘attr.myHilite’) myHiliteDirective = new … Read more

Angular2 Styles in a Directive

You can use host binding to bind to style attributes: @Directive({ selector: ‘[myHighlight]’, host: { ‘[style.background-color]’: ‘”yellow”‘, } }) or @Directive({ selector: ‘[myHighlight]’, }) class MyDirective { @HostBinding(‘style.background-color’) backgroundColor:string = ‘yellow’; }

Detect when input value changed in directive

You need to make an input property of input and then use the ngOnChanges hook to tell when the input property changes. @Directive({ selector: ‘[number]’ }) export class NumberDirective implements OnChanges { @Input() public number: any; @Input() public input: any; ngOnChanges(changes: SimpleChanges){ if(changes.input){ console.log(‘input changed’); } } } Plunkr Stackblitz

Implementing autocomplete

Update: This answer has led to the development of ng2-completer an Angular2 autocomplete component. This is the list of existing autocomplete components for Angular2: ng2-completer ng2-auto-complete ng2-typeahead Credit goes to @dan-cancro for coming up with the idea Keeping the original answer for those who wish to create their own directive: To display autocomplete list we … Read more