Created
February 20, 2017 12:33
-
-
Save seveves/9e086ee39c5fa1b6a3457611d316a807 to your computer and use it in GitHub Desktop.
Angular Checkbox
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 { AfterViewChecked, Component, EventEmitter, Input, Output } from '@angular/core'; | |
@Component({ | |
selector: 'seveves-checkbox', | |
template: ` | |
<fieldset class="form-group"> | |
<div class="form-check"> | |
<label class="form-check-label" [for]="name"> | |
<input type="checkbox" | |
[id]="name" | |
class="form-check-input mr-3" | |
[(ngModel)]="check">{{label}} | |
</label> | |
</div> | |
</fieldset> | |
`, | |
}) | |
export class CheckBox implements AfterViewChecked { | |
@Output() public checkChange = new EventEmitter<boolean>(); | |
@Input() public name: string; | |
@Input() public label: string; | |
@Input() public get check() { | |
return this.checkedValue; | |
} | |
public set check(val) { | |
this.checkedValue = val; | |
if (this.viewChecked) { | |
this.checkChange.emit(this.check); | |
} | |
} | |
private viewChecked: boolean; | |
private checkedValue: boolean; | |
public ngAfterViewChecked() { | |
this.viewChecked = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment