In ionic 2 version:
- Cordova CLI: 6.4.0,
- Ionic Framework Version: 2.0.0-rc.0,
- Ionic CLI Version: 2.1.0,
- Ionic App Lib Version: 2.1.0-beta.1,OS:
- Node Version: v6.7.0
With Ionic 2 FORM, the input: <ion-datetime>
happens to be slow (see here).
I want to go around it and use the "cordova-plugin-datepicker" instead. I have many question about it to make it work. But I'll start here with the first step I need to achieve: To implement a custom selector that can be use as a <ion-[something for a form input]>
tag.
To start with here, we are just going to try to implement the tag <ion-datetime>
thru another component.
I've found similar issue here. It tells to import:import {IONIC_DIRECTIVES} from 'ionic-angular';
and to add in the @Component
annotation the metadata: directives: [IONIC_DIRECTIVES]
. But in Angular 2 documentation the metadata directives
does not exist anymore. And I get an error if I try that.
Now my code:
I have a User form page:
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NavController, NavParams } from 'ionic-angular';
import { NativeDatePickerTag } from '../../custom-components/native-date-picker/native-date-picker';
@Component({
selector:'user-form',
templateUrl: 'user-form.html',
providers: [Validators]
})
export class UserFormPage {
private readonly PAGE_TAG:string = "UserFormPage";
public birthdate:any;
public userForm:FormGroup;
constructor(public navCtrl: NavController, public navParams: NavParams, public fb:FormBuilder, public validators:Validators){}
public updateUserData = () => {
console.log(this.userForm.value);
}
ionViewDidLoad(){
console.log(this.PAGE_TAG + " ionViewDidLoad() starts");
this.userForm = this.fb.group({
birthday: [this.birthdate,Validators.required],
});
}
}
In my 'user-form.html' it looks like this:
<ion-content>
<form (ngSubmit)="updateUserData()" [formGroup] = "userForm" >
<ion-item>
<ion-label stacked>Birthdate</ion-label>
<native-date-picker [controlName]="birthday"></native-date-picker>
</ion-item>
<button ion-button type="submit" block>Submit</button>
</form>
</ion-content>
And my custom component NativeDatePickerTag (again, this a proof of concept not yet implementing the cordova-plugin-datepicker) :
import { Component, Input, ViewChild, ElementRef } from '@angular/core';
import { Platform } from 'ionic-angular';
import { FormGroup, FormControl } from '@angular/forms';
@Component({
selector: 'native-date-picker',
template: `
<ion-datetime [formControlName]='this._controlName'></ion-datetime>
`
})
export class NativeDatePickerTag {
private readonly COMPONENT_TAG = "NativeDatePickerObj";
public _controlName: FormControl;
@Input () set controlName(newControl: FormControl){
this._controlName = newControl;
}
constructor(public platform:Platform){
}
}
If I run the code like that, it tells in the console.log:
formControlName must be used with a parent formGroup directive
I don't understand why it does not take into account the formGroup
the selector native-date-picker
is embedded in inside 'user-form.html'. So I've tried to pass the formGroup
from 'customer-form.html' to correct this error.
In 'user-form.html' I've changed,
<native-date-picker [controlName]="birthday"></native-date-picker>
with:
<native-date-picker [groupName]="userForm" [controlName]="birthday"></native-date-picker>
And in NativeDatePickerTag, I changed the annotation with:
@Component({
selector: 'native-date-picker',
template: `<div [formGroup]='this._formGroup'>
<ion-datetime [formControlName]='this._controlName'></ion-datetime>
</div>
`
})
And I added inside my class NativeDatePickerTag the following: public _formGroup: FormGroup;
@Input () set groupName(newGroup: FormGroup){
this._formGroup = newGroup;
}
Now I get in console.log:
Cannot find control with unspecified name attribute
I really don't understand what I am doing wrong. Could anyone, with experience regarding this topic,give me some directions?
I found a solution, the key was to understand how works ControlValueAccessor interface
I did that by reading thru those link:
As requested here is the code:
The native-date-picker.ts:
And native-date-picker.html:
And in the HTML template of the component containing a form that calls it, to respect the
@input
that need to be given to the class NativeDatePicker, it must look like that: