Skip to content

Commit

Permalink
feat(core): introduce NgInit directive
Browse files Browse the repository at this point in the history
  • Loading branch information
trotyl committed Apr 8, 2018
1 parent fd219a0 commit a02d443
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Angular extensions powered by community.
+ `@angular-contrib/common`
+ [`<ng-host>`][NgHost]
+ [`ngForIn`][NgForIn]
+ [`ngInit`][NgInit]
+ [`ngNoCheck`][NgNoCheck]
+ [`ngSwitchCaseContinue`][NgSwitchContinue]

Expand Down Expand Up @@ -70,6 +71,7 @@ class AppComponent { }
[IterableDiffersExtensibility]: https://github.com/trotyl/angular-contrib/tree/master/packages/core/iterable-differs
[KeyValueDiffersExtensibility]: https://github.com/trotyl/angular-contrib/tree/master/packages/core/key-value-differs
[NgForIn]: https://github.com/trotyl/angular-contrib/tree/master/packages/common/for-in
[NgNoCheck]: https://github.com/trotyl/angular-contrib/tree/master/packages/common/no-check
[NgInit]: https://github.com/trotyl/angular-contrib/tree/master/packages/common/init
[NgHost]: https://github.com/trotyl/angular-contrib/tree/master/packages/common/host
[NgNoCheck]: https://github.com/trotyl/angular-contrib/tree/master/packages/common/no-check
[NgSwitchContinue]: https://github.com/trotyl/angular-contrib/tree/master/packages/common/switch-continue
1 change: 1 addition & 0 deletions packages/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './for-in/index';
export * from './host/index';
export * from './init/index';
export * from './no-check/index';
export * from './switch-continue/index';
35 changes: 35 additions & 0 deletions packages/common/init/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# NgInit

Helper directive to perform side-effects in template.

## Type

**Directive**

## Provenance

+ https://github.com/angular/angular/issues/6585

## NgModule

`@angular-contrib/core#InitModule`

## Usage

```typescript
@Component({
template: `
<p (ngInit)="value = 'Bar'">
Will be Bar: {{ value }}
</p>
`
})
class MyComponent {
value = 'Foo';
}
```

## Note

+ Please do not use this when possible;
+ The order is equivant to `OnInit` hook on the given element;
2 changes: 2 additions & 0 deletions packages/common/init/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './init';
export * from './init.module';
8 changes: 8 additions & 0 deletions packages/common/init/init.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NgModule } from '@angular/core';
import { NgInit } from './init';

@NgModule({
declarations: [ NgInit ],
exports: [ NgInit ],
})
export class InitModule { }
34 changes: 34 additions & 0 deletions packages/common/init/init.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Component } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { InitModule } from './init.module';

describe('ngForIn', () => {
let fixture: ComponentFixture<TestComponent>;
let component: TestComponent;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [TestComponent],
imports: [InitModule],
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
});

it('should perform statement during init', () => {
fixture.detectChanges();

expect(fixture.nativeElement.textContent).toBe(`2`);
});
});

@Component({
selector: 'test-cmp',
template: `<p (ngInit)="value = 2">{{ value }}</p>`,
})
class TestComponent {
value = 1;
}
12 changes: 12 additions & 0 deletions packages/common/init/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Directive, EventEmitter, OnInit, Output } from '@angular/core';

@Directive({
selector: '[ngInit]',
})
export class NgInit implements OnInit {
@Output() ngInit = new EventEmitter<void>();

ngOnInit(): void {
this.ngInit.emit();
}
}

0 comments on commit a02d443

Please sign in to comment.