Angular – Use pipes in services and components

As usual in Angular, you can rely on dependency injection: import { DatePipe } from ‘@angular/common’; class MyService { constructor(private datePipe: DatePipe) {} transformDate(date) { return this.datePipe.transform(date, ‘yyyy-MM-dd’); } } Add DatePipe to your providers list in your module; if you forget to do this you’ll get an error no provider for DatePipe: providers: [DatePipe,…] … Read more

Angular 2 Scroll to top on Route Change

Angular 6.1 and later: Angular 6.1 (released on 2018-07-25) added built-in support to handle this issue, through a feature called “Router Scroll Position Restoration”. As described in the official Angular blog, you just need to enable this in the router configuration like this: RouterModule.forRoot(routes, {scrollPositionRestoration: ‘enabled’}) Furthermore, the blog states “It is expected that this … Read more

WARNING in budgets, maximum exceeded for initial

Open angular.json file and find budgets keyword. It should look like: “budgets”: [ { “type”: “initial”, “maximumWarning”: “2mb”, “maximumError”: “5mb” } ] As you’ve probably guessed you can increase the maximumWarning value to prevent this warning, i.e.: “budgets”: [ { “type”: “initial”, “maximumWarning”: “4mb”, <=== “maximumError”: “5mb” } ] What does budgets mean? A performance … Read more

Triggering change detection manually in Angular

Try one of these: ApplicationRef.tick() – similar to AngularJS’s $rootScope.$digest() — i.e., check the full component tree NgZone.run(callback) – similar to $rootScope.$apply(callback) — i.e., evaluate the callback function inside the Angular zone. I think, but I’m not sure, that this ends up checking the full component tree after executing the callback function. ChangeDetectorRef.detectChanges() – similar … Read more

(change) vs (ngModelChange) in angular

(change) event bound to classical input change event. https://developer.mozilla.org/en-US/docs/Web/Events/change You can use (change) event even if you don’t have a model at your input as <input (change)=”somethingChanged()”> (ngModelChange) is the @Output of ngModel directive. It fires when the model changes. You cannot use this event without ngModel directive. https://github.com/angular/angular/blob/master/packages/forms/src/directives/ng_model.ts#L124 As you discover more in the … Read more

How can I get new selection in “select” in Angular 2?

If you don’t need two-way data-binding: <select (change)=”onChange($event.target.value)”> <option *ngFor=”let i of devices”>{{i}}</option> </select> onChange(deviceValue) { console.log(deviceValue); } For two-way data-binding, separate the event and property bindings: <select [ngModel]=”selectedDevice” (ngModelChange)=”onChange($event)” name=”sel2″> <option [value]=”i” *ngFor=”let i of devices”>{{i}}</option> </select> export class AppComponent { devices=”one two three”.split(‘ ‘); selectedDevice=”two”; onChange(newValue) { console.log(newValue); this.selectedDevice = newValue; // … … Read more

What is the difference between declarations, providers, and import in NgModule?

Angular Concepts imports makes the exported declarations of other modules available in the current module declarations are to make directives (including components and pipes) from the current module available to other directives in the current module. Selectors of directives, components or pipes are only matched against the HTML if they are declared or imported. providers … Read more

Error when trying to inject a service into an angular component “EXCEPTION: Can’t resolve all parameters for component”, why?

Import it from the file where it is declared directly instead of the barrel. I don’t know what exactly causes the issue but I saw it mentioned several times (probably some kind of circular dependency). It should also be fixable by changing the order of the exports in the barrel (don’t know details, but was … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)