Angular 4: How to read content of text file with HTTPClient

Try like this : this.http.get(‘app/files/1.txt’).subscribe(data => { console.log(data.text()); }) The CLI can’t access docments inside the app directory your project. if you move to text document you can access the text file like assets/1.txt. if you want to access document inside the app directory you need to add path in assets array in the .angular-cli.json … Read more

How to add form validation pattern in Angular 2?

Now, you don’t need to use FormBuilder and all this complicated valiation angular stuff. I put more details from this (Angular 2.0.8 – 3march2016): https://github.com/angular/angular/commit/38cb526 Example from repo : <input [ngControl]=”fullName” pattern=”[a-zA-Z ]*”> I test it and it works 🙂 – here is my code: <form (ngSubmit)=”onSubmit(room)” #roomForm=’ngForm’ > … <input id=’room-capacity’ type=”text” class=”form-control” [(ngModel)]=’room.capacity’ … Read more

How to use angular2 built-in date pipe in services and directives script files [duplicate]

Since CommonModule does not export it as a provider you’ll have to do it yourself. This is not very complicated. 1) Import DatePipe: import { DatePipe } from ‘@angular/common’; 2) Include DatePipe in your module’s providers: NgModule({ providers: [DatePipe] }) export class AppModule { } or component’s providers: @Component({ selector: ‘home’, styleUrls: [‘./home.component.css’], templateUrl: ‘./home.component.html’, … Read more

How to place a dynamic component in a container

You can get the ViewContainerRef of the current component by or from an element in the view of the current component @Component({ selector: ‘…’, directives: [OtherComponent, FooComponent], template: ` <other-component></other-component> <foo-component #foo></foo-component> <div #div></div>` }) export class SomeComponent { // `ViewContainerRef` from an element in the view @ViewChild(OtherComponent, {read: ViewContainerRef}) other; @ViewChild(‘foo’, {read: ViewContainerRef}) foo; … Read more

Set style dynamically in Angular

[style.background-color]=”activity.status == ‘Pending’ ? ‘red’ : ‘green'” or [ngStyle]=”{‘backgroundColor’: activity.status == ‘Pending’ ? ‘red’ : ‘green’ }” For your rendering result see also In RC.1 some styles can’t be added using binding syntax

Angular Material2 theming – how to set app background?

If you want to change the theme’s background color for the entire app in a clean way, you can override your theme with the following. // Set custom background color $custom-background-color: map_get($mat-blue-grey, 50); // -or- Can set colour by hex value too $custom-background-color: #628cc9; $background: map-get($theme, background); $background: map_merge($background, (background: $custom-background-color)); $theme: map_merge($theme, (background: $background)); … Read more

`[(ngModel)]` vs `[(value)]`

ngModel is a directive that allows your input to participate in a form (but works also without a form) value is a property you can bind a value to with [value]=”name” while (valueChange)=”…” doesn’t work, because the <input> element doesn’t have an @Output() valueChange; therefore [(value)]=”…” is invalid. [(ngModel)]=”name” is the shorthand for [ngModel]=”name” (ngModelChange)=”name … Read more