components icon indicating copy to clipboard operation
components copied to clipboard

Support @angular/forms

Open aquinum6 opened this issue 10 years ago • 8 comments

It seems that material does not work with new forms. I have used formControlName directive and I got error that there is no value accessor for certain field in my case 'email':

No value accessor for 'email'

I have changed import in input/index.js from:

var common_1 = require('@angular/common');

to:

var common_1 = require('@angular/forms');

and it seems to start working.

aquinum6 avatar Jun 16 '16 09:06 aquinum6

@aquinum6 Can you detail a bit more what you've done? Importing REACTIVE_FORM_DIRECTIVES, and so on from @angular/forms didn't help.

0cv avatar Jun 20 '16 15:06 0cv

@Krisa you might find this helpful for new forms

http://stackoverflow.com/questions/37871271/how-to-migrate-angular-2-rc-1-or-earlier-forms-to-angular-2-rc-2-new-forms/37902014#37902014

For angular material to work you need to make below changes:

Replace var common_1 = require('@angular/common'); with var common_1 = require('@angular/forms');

in the following files:

node_modules/@angular2-material/checkbox/checkbox.js node_modules/@angular2-material/input/input.js node_modules/@angular2-material/radio/radio.js node_modules/@angular2-material/slide-toggle/slide-toggle.js

naveedahmed1 avatar Jun 20 '16 16:06 naveedahmed1

@naveedahmed1 ahh indeed, this did the trick. I didn't come on the idea to modify the source file directly, thank you!

0cv avatar Jun 20 '16 18:06 0cv

@Krisa You should read the official document too. https://docs.google.com/document/u/1/d/1RIezQqE4aEhBRmArIAS1mRIZtWFf6JxN_7B4meyWK0Y/pub

Drol avatar Jun 21 '16 07:06 Drol

@Drol, I did, that's now working. My only problem was, as referred before, the angular2-material source files I "forgot" to modify.

0cv avatar Jun 21 '16 07:06 0cv

Though replacing

var common_1 = require('@angular/common'); with var common_1 = require('@angular/forms');

in material components source files seems to be working for other components but doesn't seems to work for Checkboxes. This change does remove the ValueAccessor error on form load, but when the form is submitted it posts below object in case of checkbox if its checked:

myCheckbox: MdCheckboxChange
checked:true
source:MdCheckbox
__proto__:Object
constructor:MdCheckboxChange()
__proto__:Object

For your information @jelbourn and @kara

naveedahmed1 avatar Jun 21 '16 18:06 naveedahmed1

@naveedahmed1 It seems that valueAccessor subscribe to (change) whole event in checkbox:

MdCheckbox.prototype.registerOnChange = function (fn) {
    if (this._changeSubscription) {
        this._changeSubscription.unsubscribe();
    }
    this._changeSubscription = this.change.subscribe(fn);
};

to fix this just map change to proper value:

MdCheckbox.prototype.registerOnChange = function (fn) {
        if (this._changeSubscription) {
            this._changeSubscription.unsubscribe();
        }
        this._changeSubscription = this.change
        .map(function (v) {
            return v.checked;
        })
        .subscribe(fn);
    };

It works for me.

aquinum6 avatar Jun 22 '16 11:06 aquinum6

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.