Skip to content

Instantly share code, notes, and snippets.

@zimejin
zimejin / AngularDom.ts
Last active September 19, 2019 10:32
Two kinds of ViewChild and Their returns
@ViewChild('bioRef', {read: ElementRef}) private bioRef: ElementRef;
@ViewChild('passwordRef', {read: ElementRef}) private passwordRef: ElementRef;
// returns
ElementRef {nativeElement: ion-radio.md.in-item.interactive.hydrated}
// native element
<ion-radio _ngcontent-ibt-c6="" slot="start" value="fingerprint" ng-reflect-value="fingerprint" role="radio"
aria-checked="false" aria-labelledby="ion-rb-1-lbl" class="md in-item interactive hydrated"></ion-radio>
@zimejin
zimejin / simpleNotetaker.html
Created July 28, 2019 09:25
Vannila JS Note taker challenge
<!DOCTYPE html>
<html lang="en">
<body>
<html>
<head>
<title>Note Taker</title>
</head>
(focus)="myMethod()" // An element has received focus
(blur)="myMethod()" // An element has lost focus
(submit)="myMethod()" // A submit button has been pressed
(scroll)="myMethod()"
(cut)="myMethod()"
(copy)="myMethod()"
(paste)="myMethod()"
@zimejin
zimejin / rxjs.js
Created May 30, 2019 15:17
RxJS snippets
//emit value every 1s
const source = interval(1000);
const example = source.pipe(
map(val => {
if (val > 5) {
//error will be picked up by retryWhen
throw val;
}
return val;
}),
@zimejin
zimejin / utils.js
Last active August 17, 2020 13:47
array utilities
// Using slice() to Remove value from beginning and end of string.
sliceString = function (value) {
let result = value.slice(1, -1);
return result;
}
// Using splice() method to changes the contents of an array by removing
// or replacing existing elements and/or adding new elements
var months = ['Jan', 'March', 'April', 'June'];
@zimejin
zimejin / factory.js
Last active May 26, 2019 15:10
Factory design pattern
// Animal factory
const Animal = function(name){
const animal = {};
animal.name = name;
animal.walk = function(){
console.log(this.name + " walks");
}
return animal;
};
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'excerpt' })
export class ExcerptPipe implements PipeTransform {
transform(text: string, limit: number = 5) {
if (text.length <= limit) {
return text;
}
return text.substring(0, limit) + '...';
}
import { Component, OnInit } from '@angular/core';
import { UserAsyncService } from './user-async.service';
import { Observable } from 'rxjs';
@Component({
selector: 'app-user-async',
templateUrl: './user-async.component.html',
styleUrls: ['./user-async.component.scss'],
providers: [UserAsyncService]
})
export interface GoogleData {
id: number;
country: string;
zipCode: string;
}
import { async, TestBed } from '@angular/core/testing';
import { UserComponent } from './user.component';
import { UserService } from './user.service';
describe('UserComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [UserComponent]