Skip to content

Commit

Permalink
Merge master into directive-decorator/feature
Browse files Browse the repository at this point in the history
  • Loading branch information
endway committed Oct 26, 2017
2 parents 8a6c5fb + 789a9d5 commit 8911eae
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
19 changes: 19 additions & 0 deletions decorators/lifecycle_hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Lifecycle hook that is called when any data-binding property of a directive/component changes.
*/
export interface OnChanges { ngOnChanges(changes: ng.IOnChangesObject): void; }

/**
* Lifecycle hook that is called after data-bindings properties of a directive/component are initialized.
*/
export interface OnInit { ngOnInit(): void; }

/**
* Lifecycle hook that is called when a directive/component is destroyed.
*/
export interface OnDestroy { ngOnDestroy(): void; }

/**
* Lifecycle hook that is called after a component's view has been fully initialized.
*/
export interface AfterViewInit { ngAfterViewInit(): void; }
11 changes: 11 additions & 0 deletions example/src/app/component/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@ export class NgShiftComponent {
@Input() prop: string;

list: Array<string> = [];
ngAfterInitFired = false;

constructor(
private srv2: Service2,
private srv1: Service1,
private srv3: Service3
) {}

ngAfterViewInit() {
const afterViewInitSpan = document.createElement("span");
const componentHeader = document.querySelector("ng-shift-component h1");

if (componentHeader) {
afterViewInitSpan.textContent = "ngAfterViewInit called: true";
componentHeader.parentElement.insertBefore(afterViewInitSpan, componentHeader.nextSibling);
}
}

add(label: string) {
this.list.push(label);

Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {NgModuleDecorator} from "./decorators/ng-module/interfaces";
import {DirectiveDecorator} from "./decorators/directive/interfaces";

export * from "decorators/lifecycle_hooks";

/**
* NgModule decorator and metadata.
*/
Expand Down
5 changes: 5 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Metakeys} from "./models/metakeys";

export * from "./decorators/ng-module";
export * from "./decorators/directive";
export * from "./decorators/lifecycle_hooks";

interface IComponentClass extends Function {
$inject?: Array<string>;
Expand Down Expand Up @@ -99,6 +100,10 @@ export function Component<IComponentClass>(config?: {selector?: string, template
target.prototype.$onInit = target.prototype.ngOnInit;
}

if (target.prototype.ngAfterViewInit) {
target.prototype.$postLink = target.prototype.ngAfterViewInit;
}

if (target.prototype.ngOnChanges) {
target.prototype.$onChanges = target.prototype.ngOnChanges;
}
Expand Down
6 changes: 6 additions & 0 deletions test/component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {Component} from "../index";
})
class Test {
ngOnInit() {}
ngAfterViewInit() {}
ngOnChanges() {}
ngOnDestroy() {}
}
Expand All @@ -31,6 +32,11 @@ describe("Component decorator", function() {
expect(instance.$onInit).toEqual(instance.ngOnInit);
});

test("should link `ngAfterViewInit` to `$postLink`", function () {
expect(instance.$postLink).toBeDefined();
expect(instance.$postLink).toEqual(instance.ngAfterViewInit);
});

test("should link `ngOnChanges` to `$onChanges`", function () {
expect(instance.$onChanges).toBeDefined();
expect(instance.$onChanges).toEqual(instance.ngOnChanges);
Expand Down

0 comments on commit 8911eae

Please sign in to comment.