Get reference to a directive used in a component

You can use exportAs property of the @Directive annotation. It exports the directive to be used in the parent view. From the parent view, you can bind it to a view variable and access it from the parent class using @ViewChild(). Example With a plunker: @Directive({ selector:'[my-custom-directive]’, exportAs:’customdirective’ //the name of the variable to access … Read more

Angular exception: Can’t bind to ‘ngForIn’ since it isn’t a known native property

I typed in instead of of in the ngFor expression. Befor 2-beta.17, it should be: <div *ngFor=”#talk of talks”> As of beta.17, use the let syntax instead of #. See the UPDATE further down for more info. Note that the ngFor syntax “desugars” into the following: <template ngFor #talk [ngForOf]=”talks”> <div>…</div> </template> If we use … Read more

What is the difference between component and directive?

Basically there are three types of directives in Angular2 according to documentation. Component Structural directives Attribute directives Component It is also a type of directive with template,styles and logic part which is most famous type of directive among all in Angular2. In this type of directive you can use other directives whether it is custom … Read more

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

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’; }

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

Angular 2 Read More Directive

I made a version that uses character length rather than div size. import { Component, Input, ElementRef, OnChanges} from ‘@angular/core’; @Component({ selector: ‘read-more’, template: ` <div [innerHTML]=”currentText”> </div> <a [class.hidden]=”hideToggle” (click)=”toggleView()”>Read {{isCollapsed? ‘more’:’less’}}</a> ` }) export class ReadMoreComponent implements OnChanges { @Input() text: string; @Input() maxLength: number = 100; currentText: string; hideToggle: boolean = true; … Read more