Skip to content

Instantly share code, notes, and snippets.

@sclausen
Created December 11, 2018 08:20
Show Gist options
  • Save sclausen/e3ecc6c7571c02cc1af1bea2d689db26 to your computer and use it in GitHub Desktop.
Save sclausen/e3ecc6c7571c02cc1af1bea2d689db26 to your computer and use it in GitHub Desktop.
AngularJS ng-init for angular
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;
}
}
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