Difference between new Observable(…) and Rx.Observable.create(…)?

There is no difference. Observable.create calls new Observable. As the manual says, Observables are created using Rx.Observable.create or a creation operator <…> Rx.Observable.create is an alias for the Observable constructor Observable.create is conventionally used, probably because it reads better in chains and conforms with other Observable static methods that create observables, too. The difference may … Read more

Change styles.scss dynamically in Angular

I would recommend following the general form of the Angular Material Website: https://github.com/angular/material.angular.io/blob/master/angular.json. It’s worth cloning this repo and playing around with it locally. There are several moving parts. In your angular.json file you’ll add the css as follows. Notice that you’ll need to compile your scss to css separately for this to work. They … Read more

Create custom pipe that is async by itself

Several commenters pointed out (rightfully so) that this violates SRP and might hurt readability. But even though that’ll make me reconsider whether I want this, I still for sure wanted to know how to do it if you want it. I found two solutions (with help from commenters, again). Composition (not recommended) Create your own … Read more

Pass an input value into a ngComponentOutlet created component?

As Reno has already mentioned, you can use an injector to inject values. For the sake of completeness, here is an example with a dynamic “title” value: export const TITLE = new InjectionToken<string>(‘app.title’); @Component({ selector: ‘app-item’, template: ‘<p>{{title}}</p>’ }) export class TitleComponent implements OnInit { @Input() title:string; constructor(@Inject(TITLE) private titleInjected: string){ } ngOnInit() { this.title … Read more

What is data-bound properties?

When you have a component @Component({ selector: ‘my-component’ }) class MyComponent { @Input() name:string; ngOnChanges(changes) { } ngOnInit() { } } you can use it like <my-component [name]=”somePropInParent”></my-component> This make name a data-bound property. When the value of somePropInParent was changed, Angulars change detection updates name and calls ngOnChanges() After ngOnChanges() was called the first … Read more

How to unit-test canActivate guard method of angular2 using Jasmine?

since no one answered my question, so I’m pasting my code snippet for the reference to help people who might get this situation. sampleLoggedIn.guard.ts import {Injectable} from ‘@angular/core’; import {Router, CanActivate} from ‘@angular/router’; import {StorageService} from ‘../storage.service’; @Injectable() export class LoggedInGuard implements CanActivate { constructor(private router: Router, private storageService: StorageService) { } /**Overriding canActivate to … Read more

Running Angular2 In Subdirectory

To host in a sub directory with apache, you can just do this: Build command similar to this: ng build –base-href /myapp/ .htaccess file would look like this RewriteEngine On RewriteBase /myapp/ Options +FollowSymLinks RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.html [L,QSA]

Open new window in new tab

The reason for window variable being undefined is the fact that you have declared a variable named window again in the local scope. According to the scoping rules of javascript/typescript, before the global variable is accessed, the local variables value is looked up. Also when you initially declare a variable, it is set to undefined, … Read more