简体   繁体   English

如何在 angular 2 中的 1 个属性绑定中绑定 2 个变量?

[英]How can I bind 2 variables inside 1 property binding in angular 2?

I'm trying to dynamically generate and use form controls from my json data.我正在尝试从我的 json 数据动态生成和使用表单控件。 A simple experiment I came up with to figure out the mechanics I need to apply goes as follows.我想出一个简单的实验来找出我需要应用的机制,如下所示。

variables defined in class类中定义的变量

demoA: string         = 'name';
demoB: Array<string>  = ['city', 'state'];
demoC: FormGroup      = new FormGroup({});

function for grabbing properties from demoA and demoB and converting into FomControl s用于从demoAdemoB获取属性并转换为FomControl

loadStuff(){
    let a = this.demoA;
    let b = this.demoB;
    let ab: Array<string> = [];

    ab.push(a);
    b.forEach( bb => {ab.push(bb)} );
    console.log(ab);

    ab.forEach( ctrl => this.demoC.addControl(ctrl, new FormControl('')) );

    console.log( this.demoC.value );
}

Now the value of demoC is现在demoC的价值是

demoC: {name:'', city:'', state:''}

Due to the fact that I'm creating this on the fly when my component loads, there's no predefined way for me to bind to it, which led me to wonder if I can bind to it inside the binding on the input something like this由于我在组件加载时即时创建它,因此没有预定义的方法可以绑定到它,这让我想知道我是否可以在输入的绑定中绑定到它这样的东西

<input type="text" [(ngModel)]="demoC.{{demoA}}" />

of course that didn't work, neither did当然那没有用,也没有用

<input type="text" [(ngModel)]="demoC.[demoA]" />

<input type="text" [(ngModel)]="demoC.[(demoA)]" />

<input type="text" [(ngModel)]="demoC.(demoA)" />

<input type="text" [(ngModel)]="(demoC)+'.'+(demoA)" />

<input type="text" [(ngModel)]="[(demoC)+'.'+{{demoA}}]" />

<input type="text" [(ngModel)]="('demoC.'+{{demoA}})" />

<input type="text" [(ngModel)]="['demoC.'+{{demoA}}]" />

<input type="text" [(ngModel)]="['demoC.'+[demoA]]" />

<input type="text" [(ngModel)]="[('demoC.')+[demoA]]">

If I want the result to be demoC.name How do I go about doing this?如果我希望结果是demoC.name我该怎么做?

Why {{}} syntax ?为什么是{{}}语法? You can bind simply using [(ngModel)]=demoC[demoA] thi syntax for dynamic property .您可以简单地使用[(ngModel)]=demoC[demoA]动态属性的语法进行绑定。 But you have mixed two approaches here Simple Form approach vs Reactive Form approach.但是您在这里混合了两种方法,简单形式方法与反应形式方法。 If you want to work with FormGroup I think it will be better to use FormControlName directives instead of the ngModel .如果您想使用FormGroup我认为使用FormControlName指令而不是ngModel会更好。 Or if you want to use ngModel I think you don't need to use FormGroup with it.或者,如果您想使用ngModel我认为您不需要将FormGroup与它一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Angular 2中定义全局变量,我可以将它们用于模板中的属性绑定? - How to define global variables in Angular 2 in a way that I can use them for property binding in templates? 如何将对象的属性绑定到 Angular 中的另一个对象内部? - How can i bind a property of an object inner another object in Angular? 如何在angular2中绑定投影内容中的属性? - How can I bind to property in projected content in angular2? 如何将 angular 属性绑定到上传的文件? - how can i bind angular property to uploaded file? 角度:在[innerHtml]中绑定变量 - Angular: bind variables inside [innerHtml] 我们可以在Angular绑定的表达式内声明变量吗? - Can we declare variables inside the expression of an Angular binding? 我如何绑定div ID以动态地将c3 js图表绑定到angular2中的div中 - How I can bind the div id for dynamically for binding c3 js chart into the div in angular2 Angular:如何更改 ngFor 内组件的属性值 - Angular: How can I change the value of a property of a component inside an ngFor 无法在Angular 4中绑定属性 - Can't Bind Property in Angular 4 在 Angular 中,我可以将组件发出的 output 直接绑定到属性吗? - In Angular, can I bind the emited output of a component directly to a property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM