最佳实践与填坑的积累

1. 删除由Angular组件创建的Host HTML元素选择器

写一个remove-host 指令

//remove the host of avatar to be rendered as svg
@Directive({
    selector: '[remove-host]'
})
class RemoveHost {
    constructor(private el: ElementRef) {
    }
    //wait for the component to render completely
    ngOnInit() {
        var nativeElement: HTMLElement = this.el.nativeElement,
            parentElement: HTMLElement = nativeElement.parentElement;
        // move all children out of the element
        while (nativeElement.firstChild) {
            parentElement.insertBefore(nativeElement.firstChild, nativeElement);
        }
        // remove the empty element(the host)
        parentElement.removeChild(nativeElement);
    }
}

使用方式:`` 来自stackoverflow

2.判断<ng-content>为空

Wrap ng-content in an HTML element like a div to get a local reference to it, then bind the ngIfexpression to ref.childNodes.length == 0:

template: `<div #ref><ng-content></ng-content></div> 
           <span *ngIf="ref.childNodes.length == 0">
              Display this if ng-content is empty!
           </span>`

来自stackoverflow

进一步阅读和参考

Angular

TypeScript

General Coding Practice and Functional Programming

RxJS, Reactive Programming and Observables

Observables are known for having a steep learning curve due to the fact that it requires a different way of thinking. Here are some helpful topics about working with and understanding reactive programming using the observable model.

  • RxJS 5 Observables Reference - Reference material on RxJS 5 Observables. There are many breaking changes from RxJS 4-> 5 so please use documentation on version 5.
  • RxMarbles - Quick references for visualizing observable value fulfillment through the use of marble diagrams
  • How to debug RxJS code - Blog post explaining how to read and use marble diagrams
  • RxJS 5 Thinking Reactively - Talk from RxJS 5 product lead on how to approach development using RxJS 5
  • Learn RxJS - Example driven guide to RxJS

Redux and ngrx

Keeping up to date

Other

下一节:拦截 zone 事件是棘手的,因为 zone 是在运行时生成的,并且标准方法(如子类化和猴子补丁)只有在设计时已知父 zone时才起作用。 为了更好地演示这个问题,我们假设想在run()方法的前后进行拦截,这样我们可以测量执行时间和在控制台记录 zone。