Created
December 11, 2018 08:20
-
-
Save sclausen/e3ecc6c7571c02cc1af1bea2d689db26 to your computer and use it in GitHub Desktop.
AngularJS ng-init for angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core'; | |
import { ComponentFixture, TestBed } from '@angular/core/testing'; | |
import { OnInitDirective } from './on-init.directive'; | |
describe('OnInitDirective', () => { | |
let component: TestOnInitComponent; | |
let fixture: ComponentFixture<TestOnInitComponent>; | |
beforeEach(() => { | |
TestBed.configureTestingModule({ | |
declarations: [TestOnInitComponent, OnInitDirective] | |
}); | |
fixture = TestBed.createComponent(TestOnInitComponent); | |
component = fixture.componentInstance; | |
}); | |
it('the passed method should be executed on creating the dom element', () => { | |
fixture.detectChanges(); | |
expect(component.isInitialized).toBe(true); | |
}); | |
}); | |
@Component({ | |
template: `<span (onInit)="shouldBeExecuted()"></span>` | |
}) | |
class TestOnInitComponent { | |
public isInitialized = false; | |
public shouldBeExecuted(): void { | |
this.isInitialized = true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Directive, | |
EventEmitter, | |
OnInit, | |
Output | |
} from '@angular/core'; | |
@Directive({ | |
selector: '[onInit]' | |
}) | |
export class OnInitDirective implements OnInit { | |
@Output() | |
public onInit: EventEmitter<void> = new EventEmitter(); | |
public ngOnInit() { | |
this.onInit.emit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment