Build Angular2 HTML and TypeScript to a single file

If you’re using the default Angular2 tsconfig.json with SystemJS loader: “module”: “system”, “moduleResolution”: “node”, … You can leave all the heavy work on SystemJS Build Tool. This is for example how I do it in my projects using gulp: Compile *.ts and inline *.html templates I’m using gulp-angular-embed-templates right away to inline all templateUrl into … Read more

Angular 2 router resolve with Observable

Don’t call subscribe() in your service and instead let the route subscribe. Change return this.searchService.searchFields().subscribe(fields => { to import ‘rxjs/add/operator/first’ // in imports return this.searchService.searchFields().map(fields => { … }).first(); This way an Observable is returned instead of a Subscription (which is returned by subscribe()). Currently the router waits for the observable to close. You can … Read more

Generic email validator

You can do only using html: <md-input-container class=”md-icon-float md-block” flex-gt-sm> <label>Email</label> <input md-input id=”contact-email” type=”text” ngControl=”email” #email=”ngForm” [(ngModel)]=”contact.email” required pattern=”^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$”> <div class=”md-errors-spacer” [hidden]=”email.valid || email.untouched”> <div class=”md-char-counter” *ngIf=”email.errors && email.errors.required”> Email is required </div> <div class=”md-char-counter” *ngIf=”email.errors && email.errors.pattern”> Email is invalid </div> </div> </md-input-container>

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

Resetting a form in Angular 2 after submit

>= RC.6 Support resetting forms and maintain a submitted state. console.log(this.form.submitted); this.form.reset() or this.form = new FormGroup()…; importat update To set the Form controls to a state when the form is created, like validators, some additional measurements are necessary In the view part of the form (html) add an *ngIf to show or hide the … Read more

Angular 6 does not add X-XSRF-TOKEN header to http request

The problem once again is Angular’s poor documentation. The fact is, Angular will add the X-XSRF-TOKEN header only if the XSRF-TOKEN cookie was generated server-side with the following options: Path = / httpOnly = false (this is very important, and fully undocumented) Besides, the Angular app and the URL being called must reside on the … Read more

HttpClient POST request using x-www-form-urlencoded

You’re posting JSON data to the API instead of form data. The snippet below should work. login(username, password): Observable<any> { const body = new HttpParams() .set(‘username’, username) .set(‘password’, password); return this.http.post(‘/login’, body.toString(), { headers: new HttpHeaders() .set(‘Content-Type’, ‘application/x-www-form-urlencoded’) } ); }

Filtering specific column in Angular Material table in angular 5 [duplicate]

Working filters on each column, demo link Stackblitz. To filter specific column in mat-table, add a search field for the column as below; <mat-form-field class=”filter” floatLabel=”never”> <mat-label>Search</mat-label> <input matInput [formControl]=”nameFilter”> </mat-form-field> And we connect the inputs to FormControls from the ReactiveFormsModule. filterValues = { name: ”, id: ”, colour: ”, pet: ” }; And we … Read more

Migrating Angular 4.x to Angular 5 [duplicate]

You need to update all angular provided packages to their latest versions as follows: npm install [email protected] –save-dev npm install @angular/common@latest @angular/compiler@latest @angular/compiler-cli@latest @angular/core@latest @angular/forms@latest @angular/http@latest @angular/platform-browser@latest @angular/platform-browser-dynamic@latest @angular/platform-server@latest @angular/router@latest @angular/animations@latest –save This should do all the necessary. Plus you could also update your angular cli that ships with angular 5 as standard version as … Read more

Class inheritance support

Add a provider to each derived component, aliasing the base component to the derived component: @Component({ selector: ‘child-comp2’, template: ‘<div>child component #2</div>’, providers: [{provide: BaseComponent, useExisting: forwardRef(() => ChildComponent2) }] }) export class ChildComponent2 extends BaseComponent { } @Component({ selector: ‘child-comp1’, template: ‘<div>child component #1</div>’, providers: [{provide: BaseComponent, useExisting: forwardRef(() => ChildComponent1) }] }) export … Read more