Skip to content

Instantly share code, notes, and snippets.

@niisar
Created May 19, 2016 03:44
Show Gist options
  • Select an option

  • Save niisar/45a9c3e451e77e4e0f79b617d42dea55 to your computer and use it in GitHub Desktop.

Select an option

Save niisar/45a9c3e451e77e4e0f79b617d42dea55 to your computer and use it in GitHub Desktop.
DI - Class constructor injection
import { Component } from '@angular/core';
import { Control, Validators, FormBuilder } from '@angular/common';
@Component({
moduleId: module.id,
selector: 't3-app',
templateUrl: 't3.component.html',
styleUrls: ['t3.component.css']
})
export class T3AppComponent {
form;
constructor(private formBuilder: FormBuilder) {}
ngOnInit(){
this.form = this.formBuilder.group({
'medium': new Control('Movies'),
'name': new Control('',Validators.compose([
Validators.required,
Validators.pattern('[\\w\\-\\s\\/]+'),
])),
'year': new Control('',this.yearValidation)
});
}
yearValidation(control){
if(control.value.trim().length === 0) return null;
var year = parseInt(control.value);
var minYear = 2000;
var maxYear = 2010;
if(year >= minYear && year <= maxYear) return null;
return {'year': {'min':minYear, 'max':maxYear}}
}
title:any;
onSubmit(data){
this.title = data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment