0

我有以下代码片段

.html

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

。镖

class Age
{
  int years = 0;
}

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

    age = new Age()
  }

...

}

我尝试运行该应用程序时出现以下错误(部分)

>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)
...

似乎 type="num" 没有被处理。我怀疑年龄 int 也可能是一个问题,因为它是一个 int 但需要一个字符串。从 sting 返回到 int 的反向转换也可能是一个问题。

任何帮助表示赞赏。

谢谢

4

1 回答 1

0

该字段必须是 type String。Angular 不会转换该值。

类似于Polymer dart:数据将整数值绑定到字符串属性

也可以看看

自定义值访问器可能会有所帮助。有关示例,请参见此 Plunker
该组件使用, ,Address实现ControlValueAccessorwriteValue(v)registerOnChange(fn)registerOnTouched(fn)

于 2016-02-13T22:29:45.207 回答