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

0

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

3 Comments

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)
I found the use of ControlValueAccessor at http://plnkr.co/edit/aggee6An1iHfwsqGoE3q?p=preview, but would require a port to Dart as some Classes seems different.
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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.