Skip to content

Instantly share code, notes, and snippets.

@klinki
klinki / crisis-detail-component-01-param.ts
Last active May 3, 2017 21:21
Angular Router Pain - Examples
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Crisis } from './crisis.service';
@Component({
template: `
<div *ngIf="crisis">
<h3>"{{ editName }}"</h3>
<div>
<label>Id: </label>{{ crisis.id }}</div>
@klinki
klinki / expand-collapse.component.html
Last active April 19, 2017 11:23
Expand/collapse component
<div>
<a [ngClass]="{'active': isActive}" (click)="toggle()">{{tabTitle}}</a>
<ng-content *ngIf="isActive"></ng-content>
</div>
@klinki
klinki / route-flattener.service.ts
Created March 22, 2017 10:00
Angular Route Flattener
import {ActivatedRouteSnapshot, UrlSegment, Params, Data, Route} from "@angular/router";
import {Type, Injectable} from "@angular/core";
import {Subject, ReplaySubject} from "rxjs";
@Injectable()
export class RouteFlattenerService {
public getFlattenedRouteData(route: ActivatedRouteSnapshot): FlattenedRouteData {
let downLevel = this.getActivatedRouteSnapshotWithChildren(route);
downLevel.splice(0, 1);
let upLevel = this.getActivatedRouteSnapshotWithParents(route);
@klinki
klinki / RouteLinkProviderService.ts
Created December 13, 2016 13:43
RouteLinkProviderService - service which maps component into its route
import {Routes, Route} from "@angular/router";
import {Injectable} from "@angular/core";
@Injectable()
export class RouteLinkProviderService {
protected routes: Routes;
protected routesByComponents = new Map<any, any>();
constructor(routes: Routes) {
@klinki
klinki / anguar2-select-changed-stuff.ts
Created December 9, 2016 10:07
Angular 2 select with custom comparator
import {Directive, Renderer, ElementRef, Input, OnDestroy, Optional, Host} from "@angular/core";
import {SelectMultipleControlValueAccessor} from "@angular/forms";
import {looseIdentical} from "@angular/core/src/facade/lang";
import {Comparator, ComparatorCallback, CUSTOM_SELECT_MULTIPLE_VALUE_ACCESSOR} from "./custom-select.directive";
// [formControlName],select[multiple][wu-select][formControl],select[multiple][wu-select][ngModel]
@Directive({
selector: 'select[multiple][wu-select]',
host: {'(change)': 'onChange($event.target)', '(blur)': 'onTouched()'},
@klinki
klinki / select.component.html
Created September 29, 2016 14:05
Angular 2 select
<h2>One way binding</h2>
<select [ngModel]="selectedItems" (ngModelChange)="onChange($event)" multiple>
<option [ngValue]="i" *ngFor="let i of collection">{{i.name}}</option>
</select>
<h2>Two way binding</h2>
<button>Select all</button><button>Clear selection</button>
<select [(ngModel)]="selectedItems" (ngModelChange)="onChange($event)" multiple>
<option [ngValue]="i" *ngFor="let i of collection">{{i.name}}</option>
</select>