ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

bug: Ionic2 beta 10 ionic form elements do not work with @angular/forms

Open WHarris22 opened this issue 10 years ago • 30 comments

Short description of the problem:

When trying to use the Angular 2 rc 3 @angular/forms module, Ionic's form elements always throw an error when adding the formControlName directive to them. For example: <ion-input type="text" formControlName="formControl1" ></ion-input>

Putting it to normal HTML input markup works OK, for example: <input type="text" formControlName="fullName" />

What behavior are you expecting?

Ionic form elements; ion-input, ion-textarea, ion-select etc should all work with the new @angular/forms module.

Steps to reproduce:

  1. Start a new Ionic app using beta 10
  2. Add the @angular/forms module to the app
  3. In the app.ts file import the following import {disableDeprecatedForms, provideForms} from '@angular/forms';
  4. And then add the two imports to the ionicBootstrap like this ionicBootstrap(MyApp, [ disableDeprecatedForms(), provideForms() ]);
  5. Now on another page create a form using Ionic inputs and the Form Builder part of this tutorial

Thats the best I can do for reproducing because I can't find any Plunker examples set up with the required modules and Ionic beta 10.

Other information: (e.g. stacktraces, related issues, suggestions how to fix, stackoverflow links, forum links, etc)

The error written to the console log is:

EXCEPTION: Error: Uncaught (in promise): EXCEPTION: Error in build/pages/issue-form/issue-form.html:61:39 ORIGINAL EXCEPTION: No value accessor for 'control1' ORIGINAL STACKTRACE: Error: No value accessor for 'control1' at new BaseException (http://localhost:8100/build/js/app.bundle.js:2435:23) at _throwError (http://localhost:8100/build/js/app.bundle.js:37484:11) at Object.setUpControl (http://localhost:8100/build/js/app.bundle.js:37459:9) at FormGroupDirective.addControl (http://localhost:8100/build/js/app.bundle.js:37027:18) at FormControlName.ngOnChanges (http://localhost:8100/build/js/app.bundle.js:36902:32) at DebugAppView._View_IssueFormPage0.detectChangesInternal (IssueFormPage.template.js:509:56) at DebugAppView.AppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:30588:14) at DebugAppView.detectChanges (http://localhost:8100/build/js/app.bundle.js:30693:44) at DebugAppView.AppView.detectViewChildrenChanges (http://localhost:8100/build/js/app.bundle.js:30614:19) at DebugAppView.AppView.detectChangesInternal (http://localhost:8100/build/js/app.bundle.js:30599:14)

A related post on the Ionic forum

Run ionic info from terminal/cmd prompt: (paste output below)

Cordova CLI: 5.4.0 Gulp version: CLI version 3.8.11 Gulp local: Local version 3.9.1 Ionic Framework Version: 2.0.0-beta.10 Ionic CLI Version: 2.0.0-beta.25 Ionic App Lib Version: 2.0.0-beta.15 OS: Windows 8.1 Node Version: v4.4.2

WHarris22 avatar Jun 29 '16 15:06 WHarris22

Hello, thanks for opening an issue with us! We are currently aware of this issue and will be working on it. There were some pretty big breaking changes in the recent refactor of the Angular 2 forms module.

Note to self and the team: this issue may be related: https://github.com/driftyco/ionic/issues/6580

jgw96 avatar Jun 29 '16 16:06 jgw96

This may be related to(not 100% on that): https://github.com/driftyco/ionic/issues/7010

jgw96 avatar Jun 29 '16 16:06 jgw96

I don't know if there is an elegant way to support both deprecated and new forms, but I suspect this issue results from importing NG_VALUE_ACCESSOR from "@angular/common" instead of from "@angular/forms".

rapropos avatar Jun 29 '16 23:06 rapropos

@rapropos yeah, there are a good list of things that the Angular team changed in forms, but we're going through and updating everything so it works with the new forms (: Thanks for using Ionic!

jgw96 avatar Jun 30 '16 14:06 jgw96

BTW - will Ionic be supporting Angular built in validators like required, minlength, maxlength, pattern?

kokokenada avatar Jul 01 '16 13:07 kokokenada

This is fixed for me (with ion-checkbox) as of this commit.

rapropos avatar Jul 01 '16 23:07 rapropos

Yup, this should be closed with this commit: https://github.com/driftyco/ionic/commit/4d56219a4c33ca32587b83d90b014ae2e374959c

adamdbradley avatar Jul 02 '16 01:07 adamdbradley

@adamdbradley this fixed input elements, but I am getting the following build error (from master):

src/components/menu/menu.ts(617,26): error TS2339: Property 'elementRef' does not exist on type 'Backdrop'.

vidakovic avatar Jul 02 '16 10:07 vidakovic

@vidakovic just fixed: https://github.com/driftyco/ionic/commit/cac1d4f961137c7fae34adcd3ebc6ba8fecdb0b1

manucorporat avatar Jul 02 '16 15:07 manucorporat

@manucorporat thanks a ton for quick fix, made my day ;-)

vidakovic avatar Jul 02 '16 15:07 vidakovic

I am on the nightly "ionic-angular": "^2.0.0-beta.10-201606302045" and it not solved yet

cescoferraro avatar Jul 04 '16 21:07 cescoferraro

@cescoferraro the build you mention doesn't contain the fix... it's included in master 2 days later. See: https://github.com/driftyco/ionic/commit/cac1d4f961137c7fae34adcd3ebc6ba8fecdb0b1

vidakovic avatar Jul 04 '16 21:07 vidakovic

@vidakovic OK! How can I get the master from npm then?

cescoferraro avatar Jul 04 '16 21:07 cescoferraro

@cescoferraro what I did:

  • checkout the master branch with git
  • build: gulp build --typecheck
  • delete [YOURPROJECT]/node_modules/ionic-angular
  • copy the content of "dist" folder to [YOURPROJECT]/node_modules/ionic-angular

Not sure if there is a shorter way to do this, but it's not too bad.

vidakovic avatar Jul 04 '16 21:07 vidakovic

I am having the same issue but when I tried your suggestion I ran into an issue where all components referencing "ionic-angular" complains about it not found. I see this when I try to serve my app using "ionic serve" or even when I try to build my app using "ionic build". Did you run into this by any chance and if so how did you work around it?

bsakweson avatar Jul 05 '16 13:07 bsakweson

@bsakweson be aware that there are some changes (if you are coming from beta 10). E. g. the toast and modal API are used in a slightly different way.

If ionic-angular is not found at all then please double check if you copied your manual build to the correct folder under node_modules in your project directory...

vidakovic avatar Jul 05 '16 14:07 vidakovic

Incredible! Was experiencing a similar issue with ionic-angular beta 10, angular2 rc 4 and angular/forms 0.2.0. Any combination of provideForms() (injected into ionicBootstrap) and ion-range (in any component) would fire exceptions. Got latest rev from master as per comments above, and everything works perfectly now!

Thanks @vidakovic, @manucorporat and @adamdbradley for the great bug fixes and help! Saved me from a ton of headaches!

yrefaeli avatar Jul 06 '16 01:07 yrefaeli

In addition to @vidakovic's method, if you're using webpack, here's how I was able to use a locally built ionic:

  • check out with git
  • gulp prerelease
  • cd dist
  • npm link
  • go to app project directory
  • clear out node_modules/ionic-angular
  • add aliases described in this issue to webpack configuration
  • npm link ionic-angular

This way further tweaks to the local ionic build will automatically filter up without requiring constantly cleaning and copying the dist directory. I suspect something similar is possible with browserify, but I don't know.

rapropos avatar Jul 06 '16 19:07 rapropos

@vidakovic I followed your steps to generate ionic-angular from master but I can't see package.json in dist folder, any advice?

guillenotfound avatar Jul 06 '16 21:07 guillenotfound

Another option:

gulp build

and now copy/paste manually the content of /dist inside node_modules/ionic-angular

manucorporat avatar Jul 06 '16 22:07 manucorporat