How to truncate text in Angular2?

Two way to truncate text into angular. let str=”How to truncate text in angular”; 1. Solution {{str | slice:0:6}} Output: how to If you want to append any text after slice string like {{ (str.length>6)? (str | slice:0:6)+’…’:(str) }} Output: how to… 2. Solution(Create custom pipe) if you want to create custom truncate pipe import … Read more

Generate dynamic css based on variables angular

You can use ngStyle to dynamically add the css to your page from json. <div [ngStyle]=”{‘color’: variable ? ‘red’ : ‘blue’}”></div> An another example: <div md-card-avatar [ngStyle]=”{‘background-image’: ‘url(‘ + post.avatar + ‘)’, ‘background-size’: ‘cover’ }”></div> here I have loaded background image from json-data.

What is let-* in Angular 2 templates?

update Angular 5 ngOutletContext was renamed to ngTemplateOutletContext See also CHANGELOG.md @ angular/angular original Templates (<template>, or <ng-template> since 4.x) are added as embedded views and get passed a context. With let-col the context property $implicit is made available as col within the template for bindings. With let-foo=”bar” the context property bar is made available … Read more

Angular 2 Dynamically insert a component into a specific DOM node without using ViewContainerRef

When creating a component you can pass the DOM node that will act as a host element of the created component: create(injector: Injector, projectableNodes?: any[][], rootSelectorOrNode?: string|any, ngModule?: NgModuleRef): ComponentRef But since this component is not child of any other component, you have to manually attach it to ApplicationRef so you get change detection. So … Read more

Angular2 – Interpolate string with html

You can simply use [innerHTML] directive to accomplish it. http://plnkr.co/edit/6x04QSKhqbDwPvdsLSL9?p=preview import {Component, Pipe} from ‘@angular/core’ @Component({ selector: ‘my-app’, template: ` Hello my name is <span [innerHTML]=”myName”></span> `, }) export class AppComponent { myName=”<strong>Pardeep</strong>”; } Update: I checked it doesn’t work this way after RC.1 release. Let’s say to make it work with RC.4 you can … Read more

vs

Edit : Now it is documented <ng-container> to the rescue The Angular <ng-container> is a grouping element that doesn’t interfere with styles or layout because Angular doesn’t put it in the DOM. (…) The <ng-container> is a syntax element recognized by the Angular parser. It’s not a directive, component, class, or interface. It’s more like the curly braces in a … Read more

How to add “class” to host element?

This way you don’t need to add the CSS outside of the component: @Component({ selector: ‘body’, template: ‘app-element’, // prefer decorators (see below) // host: {‘[class.someClass]’:’someField’} }) export class App implements OnInit { constructor(private cdRef:ChangeDetectorRef) {} someField: boolean = false; // alternatively also the host parameter in the @Component()` decorator can be used @HostBinding(‘class.someClass’) someField: … Read more

Angular2: How is ngfor expanded

Yes, all magic happens in the compiler. Let’s take this template: <div *ngFor=”let foo of foobars”>{{foo}}</div> First it will be transformed to the following: <div template=”ngFor let foo of foobars>{{foo}}</div> And then: <template ngFor let-foo [ngForOf]=”foobars”><div>{{foo}}</div></template> In Angular2 rc.4 it looks like this First is generated ast tree node (Abstract Syntax Tree node) and then … 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