0

I have the following code snippets

.html

  <input type = "number"
         min = "0"
         max = "120"
         #yearsCtrl = "ngForm"
         [ngFormControl] = "ageForm.controls['yearsCtrl']"
         [(ngModel)] = "age.years"
         id = "years">

.dart

class Age
{
  int years = 0;
}

class AgeComponent {
....
  AgeComponent( FormBuilder fb, ModelService
  ageSrvc) {
    ageForm = fb.group( {
      'yearsCtrl': [''],
    } );
    _yearsCtrl = ageForm.controls['yearsCtrl'];

    age = new Age()
  }

...

}

My attempts to run the application gives the following errors (partial)

>EXCEPTION: type 'String' is not a subtype of type 'num' of 'value'. in [AST]
  EXCEPTION: type 'String' is not a subtype of type 'num' of 'value'. in [AST]
    (anonymous function)    
  ORIGINAL EXCEPTION: type 'String' is not a subtype of type 'num' of 'value'.
    (anonymous function)    
  ORIGINAL STACKTRACE:
    (anonymous function)    
  #0      NumberValueAccessor.writeValue (package:angular2/src/common/forms/directives/number_value_accessor.dart:38:23)
#1      setUpControl (package:angular2/src/common/forms/directives/shared.dart:34:21)
#2      NgFormControl.ngOnChanges (package:angular2/src/common/forms/directives/ng_form_control.dart:111:7)
...

It seems as if the type="num" is not being handled. I suspect the age int might be an issue also, in that it is an int but a string is required. The reverse conversion from sting back to int might also be an issue.

Any help is appreciated.

Thanks

Community
  • 1
  • 1
st_clair_clarke
  • 5,453
  • 13
  • 49
  • 75

1 Answers1

0

The field needs to be of type String. Angular doesn't convert the value.

Similar to Polymer dart: Data bind integer value to String attribute

See also

A custom value accessor might help. See this Plunker for an example.
The component Address implements ControlValueAccessor with writeValue(v), registerOnChange(fn), registerOnTouched(fn)

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Why can't a pipe be used for the conversion for the ngModel assignment above? I tried it and it results in errors. I had previously looked at my own post above but couldn't say how I would apply it to angular. I wll take a look at the ControlValueAccessor with writeValue(v) – st_clair_clarke Feb 14 '16 at 22:25
  • I found the use of ControlValueAccessor at [http://plnkr.co/edit/aggee6An1iHfwsqGoE3q?p=preview](http://plnkr.co/edit/aggee6An1iHfwsqGoE3q?p=preview), but would require a port to Dart as some Classes seems different. – st_clair_clarke Feb 14 '16 at 22:37
  • The presence of `#yearsCtrl = "ngForm" or [ngFormControl] = "ageForm.controls['yearsCtrl']"` **ALWAYS** generate the error. Is this a bug or is it that angular2 does not support the number type? – st_clair_clarke Feb 19 '16 at 02:12