简体   繁体   English

如何在Angular2中创建多个控件并将其绑定到一个字段对象?

[英]How to create and bind multiple controls to a field object in Angular2?

I have a page where the user picks a value for their preference from a combox. 我有一个页面,用户从一个combox中为他们的偏好选择一个值。 They also have the option to add more preferences, so that the number of comboxes can't be fix. 他们还可以选择添加更多首选项,以便无法修复计算机的数量。

In my TS file, I have an object that holds the data from the controls as fields. 在我的TS文件中,我有一个对象将控件中的数据保存为字段。 Up to now, I used the following trick to update it when a control changed its value. 到目前为止,当控件更改其值时,我使用以下技巧来更新它。

<span>Feature</span>
<input (change)="update('nick', $event.target.value)">
<select (change)="update('type', $event.target.value)">
  <option value="1">Uno</option>
  <option value="2">Duo</option>
  <option value="3">Tri</option>
</select>

preferences: Prefs = {};

update(property, value) {
  this.userCreated[property] = value;
}

The problem is that the object structure will change now in two ways. 问题是对象结构现在会以两种方式改变。

  1. Some fields will be encapsulated into subsections in the JSON carrier. 某些字段将封装到JSON载体的子部分中。 I can handle that by calling update('subsection', $event.target.value) followed by a manual control by if/else in the method, although I'm sure there's a better way to do that. 我可以通过调用update('subsection', $event.target.value)然后在方法中使用if / else进行手动控制来处理它,尽管我确信有更好的方法可以做到这一点。

  2. The user will be able to click a button and generate <span>Feature 2</span> , <span>Feature 3</span> etc. I'm lost at how to generate those fields (other than physically appending them in the DOM) so that they can be bound to an array in the JSON holder in the TS file. 用户将能够单击按钮并生成<span>Feature 2</span><span>Feature 3</span>等。我迷失了如何生成这些字段(除了将它们物理附加到DOM)以便它们可以绑定到TS文件中JSON holder中的数组。

I've googled it and I've seen quite a lot talking about importing Model , using ngmodel , ng-model etc. Regrettably, I haven't managed to pick up the relevant parts to perform such a binding. 我用ngmodel搜索了它,我已经看到很多关于导入Model ,使用ngmodelng-model等。遗憾的是,我还没有设法获取相关部分来执行这样的绑定。 I really want to understand why things happen and how to handle them in a proper way (that isn't an outdated approach valid in RC or betas). 我真的想了解事情发生的原因以及如何以适当的方式处理它们(这不是在RC或beta中有效的过时方法)。 Sadly, I feel that I'm not bright enough to grasp it and I need help elaborating what's already out there. 可悲的是,我觉得我不够聪明,无法掌握它,我需要帮助详细说明那里已有的东西。 Sorry in advance if it causes annoyance. 如果它引起烦恼,请提前抱歉。 (I really respect avoiding what have you tried and show us some code .) (我真的很尊重避免你尝试过的东西并向我们展示一些代码 。)

You could derive you available options from some state in your controller. 您可以从控制器中的某个状态派生出可用选项。 Then you only have to add a new option to that model and the DOM will be re-rendered with said data. 然后,您只需要为该模型添加一个新选项,DOM将使用所述数据重新呈现。

values = [{key: 1, name: 'Uno'}, {key: 2, name: 'Two'}]

<select (change)="update('type', $event.target.value)">
    <option *ngFor="let value of values" [value]="value.key">{{value.name}}</option>
</select>

adding a new value to values would rerender the dom 向值添加新values将重新渲染dom

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM