-
-
Save roblight/732a525a09c8dc259464 to your computer and use it in GitHub Desktop.
Simple Angular 2 Forms with Firebase
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 {bootstrap, Component, Decorator, View, If, For, EventEmitter} from 'angular2/angular2'; | |
import {FormBuilder, Validators, FormDirectives, ControlGroup} from 'angular2/forms'; | |
@Component({ | |
selector: 'app', | |
injectables: [FormBuilder] | |
}) | |
@View({ | |
template: ` | |
<div class="container" [control-group]="myForm"> | |
<h2>Simple Form</h2> | |
<div class="form-group" | |
[class.has-success]="myForm.controls.name.valid" | |
[class.has-error]="! myForm.controls.name.valid" | |
> | |
<label class="control-label">Name</label> | |
<input control="name" class="form-control"> | |
</div> | |
<div class="form-group" | |
[class.has-success]="myForm.controls.age.valid" | |
[class.has-error]="! myForm.controls.age.valid" | |
> | |
<label class="control-label">Age</label> | |
<input type="number" control="name" class="form-control"> | |
</div> | |
</div> | |
`, | |
directives: [FormDirectives] | |
}) | |
class AppComponent { | |
myForm:ControlGroup; | |
builder:FormBuilder; | |
constructor(b:FormBuilder) { | |
this.builder = b; | |
this.myForm = b.group({ | |
name: ["", Validators.required], // required | |
age: [""] // optional | |
}); | |
this.ref = new Firebase('https://ngforms.firebaseio.com/form'); | |
// save changes to Firebase as the form updates | |
this.myForm.valueChanges.subscribe(function(value) { | |
this.ref.set(value); | |
}.bind(this)); | |
} | |
} | |
export function main() { | |
bootstrap(AppComponent); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment