Solvednativescript angular FormBuilder to nativescript
✔️Accepted Answer
@VladimirAmiorkov pretty sure @AyWa is referring to ReactiveFormsModule FormBuilder:
https://github.com/angular/angular/blob/2.1.2/modules/%40angular/forms/src/form_providers.ts#L26-L38
http://blog.thoughtram.io/angular/2016/06/22/model-driven-forms-in-angular-2.html
I have been wanting to submit a PR to make that API work with nativescript-angular. Should be doable and I've heard many people desiring same thing.
Other Answers:
Hi @hdeshev,
The following is an excellent article which describes how to implement a custom Angular2 form control:
http://blog.thoughtram.io/angular/2016/07/27/custom-form-controls-in-angular-2.html
As currently the text-value-accessor.ts and others were implemented as an Angular Directive, which has the selector hard-coded to TextField[ngModel]..., etc. I believe can simply change the selector text to the following and test it out:
selector: "TextField[ngModel],TextField[formControlName]...
To test whether it works, can simply test with the following:
- Create a project with tns
- Modify the file app.module.ts as follows:
import { NgModule } from "@angular/core";
import { NativeScriptModule } from "nativescript-angular/platform";
import { NativeScriptFormsModule } from "nativescript-angular/forms";
import { ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from "./app.component";
@NgModule({
declarations: [AppComponent],
bootstrap: [AppComponent],
imports: [
NativeScriptModule,
NativeScriptFormsModule,
ReactiveFormsModule
]
})
export class AppModule { }
- In app.component.html, add a TextField and try to bind to a FromControl by name:
<StackLayout class="p-20" [formGroup]="loginForm">
<Label text="Tap the button" class="h1 text-center"></Label>
<Button text="TAP" (tap)="onTap()" class="btn btn-primary btn-active"></Button>
<Label [text]="message" class="h2 text-center" textWrap="true"></Label>
<TextField formControlName="usernameControl"></TextField>
<Label [text]="username" class="h2 text-center" textWrap="true"></Label>
</StackLayout>
Note that we also need to add a FormGroup to the binding, Angular2 form will need this
- In app.component.ts, add logic to build the FormGroup and FormControl for the text field:
import { Component, OnInit } from "@angular/core";
import { AbstractControl, FormControl, FormGroup, FormBuilder, Validators } from '@angular/forms';
@Component({
selector: "my-app",
templateUrl: "app.component.html",
})
export class AppComponent {
public counter: number = 16;
loginForm: FormGroup;
usernameControl: AbstractControl;
username = '';
constructor(private fb: FormBuilder) {}
ngOnInit() {
this.loginForm = this.fb.group({
username: ['', [Validators.required, Validators.minLength(3), Validators.maxLength(40)]]
});
this.usernameControl = this.loginForm.controls['username'];
}
public get message(): string {
if (this.counter > 0) {
return this.counter + " taps left";
} else {
return "Hoorraaay! \nYou are ready to start building!";
}
}
public onTap() {
this.counter--;
this.username = this.usernameControl.value;
}
}
Then test to run the application to see if it works or not.
Currently, when I run the above code, I got the following error:
CONSOLE ERROR file:///app/tns_modules/trace/trace.js:160:30: ns-renderer: Error in app.component.html:4:15 caused by: Cannot find control with name: 'usernameControl'
I believe as currently the text field value accessor doesn't pick up the attribute "formControlName", so the binding not work and Angular doesn't found any property with ControlValueAccessor implemented.
Hope this helps.
Clarence
Hello everyone,
I used FormBuilder to my webapps, and now I am trying to use it to nativescript but I thinks it’s not possible because FormBuilder need an input.
Is it possible to make it works?
thanks