问题描述
我正在创建角形的反应形式.若要将您的打字稿文件中的FormControl对象同步到html文件中的表单控件,您需要使用formControlName指令.这样做如下:
I am creating a reactive form in angular. To synchronize the FormControl object from your typescript file to control of form in html file, you need to use formControlName directive. This is done as follows:
formControlName="xyz"
其中xyz是在打字稿文件中定义的FormControl对象的键.但是在这种情况下,xyz被视为字符串,并且不会被求值.所以我的疑问是,由于xyz是FormControl对象的键,因此应该对其进行评估,并且必须将其与方括号一起使用,例如
where xyz is the key of FormControl object defined in typescript file.But in this case, xyz is treated as a string and will not get evaluated. So my doubt is that, since xyz is a key to FormControl object, so it should be evaluated and must used with square brackets like
[formControlName]="xyz"
我对FormGroup的使用方式感到满意,因为它用于方括号,例如:
I am satisfied with the way FormGroup is used because it is used with square brackets like:
[FormGroup] = "abc"
其中abc是对组件的打字稿文件中定义的FormGroup对象的对象引用.
where abc is an object reference to FormGroup object defined in typescript file of the component.
所以请解释为什么formControlName不能与方括号一起使用?
So please explain why formControlName is not used with a square bracket?
推荐答案
此行为不仅限于formControlName
,而且还限于所有角度输入/属性.它归结为您想要实现的目标以及如何定义角度样式指南.
This behaviour is not limited for formControlName
but for all angular input/attribute properties. It boils down to what you wanted to achieve and how your angular style guide is defined.
2.不带括号::当您希望angular将值作为字符串传递给formControlName
时,请不要使用[]
.在下面的示例中,name
是字符串 还声明您确定formcontrol的名称不会 动态变化.例如:formControlName = "name"
2. Without brackets: When you expect angular to pass the value just as string to formControlName
you dont use []
. In the below example name
is string also states you are sure that the name of the formcontrol doesn't change dynamically. Eg: formControlName = "name"
这篇关于在angular的反应形式中,为什么在formControlName中传递的参数作为字符串传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!