【发布时间】:2020-06-11 00:38:33
【问题描述】:
举个例子:
model.ts
export interface Item{
name: string;
age: string
}
component.ts
form: FormGroup
constructor(private fb: FormBuilder){}
onInit(){
this.form = fb.group({
name: [''],
age: ['']
})
}
exampleFn(){
const signature: Item = Object.assign({},this.form.value);
console.log(typeof(signature.age)); // <=== HERE
// output number
}
html
<form [formGroup]="form">
<input formControlName="name" type="text"/>
<input formControlName="age" type="number"/>
</form>
为什么值不是字符串?在模型中是用字符串定义的,虽然输入类型是数字,因为我想友好地显示它,但我需要一个字符串,而且我不想单独转换(在一个模型中输入了一堆 itens 的场景中思考) .
谢谢
堆栈闪电战示例
【问题讨论】:
-
是的,因为输入类型用数字填充,但我需要将其作为字符串发送到 json 对象中。但是,无需单独解析,因为这是一个具有 2 个属性的简单示例,在实际情况下,我有 100 多个属性并且可以更多。
-
但我不知道如何,正在发送号码
-
对不起,我误解了你的问题。不,您不能仅通过使用接口使其输出字符串。您需要手动转换它。
-
typescript 上的类型只是编码过程中的一个助手,但它们不会保留在构建的代码上。
-
谢谢 Elias,我会遭受手动解析????
标签: angular typescript validation