简体   繁体   English

Angular2-更新数据模型的表单值以进行多选

[英]Angular2 - Updating a form value for data model for multi-select

I am using a multi-select package for my angular project. 我正在为我的角度项目使用多选包 It allows me to bind to my reactive form model which is working fine. 它使我可以绑定到运行良好的反应形式模型。

On init of my component, I am storing the initial value from my model in a variable. 在组件初始化时,我将模型中的初始值存储在变量中。 I allow the user to enable "edit Mode" on my page which gives them the multi-select drop down. 我允许用户在我的页面上启用“编辑模式”,从而为他们提供多选下拉菜单。

In the event that they want to back out of any changes that they made (selected/removed), I need to re-set the initial value. 如果他们想撤消所做的任何更改(选择/删除),则需要重新设置初始值。

this.ruleAttributeForm.controls['AttributeValue'].setValue(this.initialValueInput, {emitEvent: true})

I tried to do something like this, which updates the model and the input box itself, however, when I open the dropdown, the values that were previously selected are still marked at selected even though they don't appear in the input. 我尝试执行类似的操作,该操作会更新模型和输入框本身,但是,当我打开下拉菜单时,即使先前选择的值未出现在输入中,也仍会标记为“选定”。

<ng-select formControlName="AttributeValue" [items]="valueOptions" [multiple]="true" bindLabel="Value" placeholder="Select one or more values" class="test"></ng-select>

The values this input selects comes from formControlName and the items to pick from come from [items] . 此输入选择的值来自formControlName ,要选择的项来自[items] The .setValue appears to work for the model / input but isn't reseting which items are available to pick from (the ones I selected before I reset the control value are marked as in use). .setValue似乎适用于模型/输入,但不会重置可供选择的项目(在重置控制值之前选择的项目被标记为正在使用)。

Is there another way I should be handeling updating my form value or is this a plugin issue? 我还有其他方法可以更新表单值吗?还是这是插件问题?

As per the docs , the each element of the list valueOptions passed to the items property should conform to the type NgOption below, 根据docs ,传递给items属性的列表valueOptions的每个元素应符合下面的NgOption类型,

export interface NgOption {
    [name: string]: any;
    selected?: boolean;
    disabled?: boolean;
    label?: string;
    value?: string;
}

It has an optional boolean selected property. 它具有一个可选的boolean selected属性。 So try adding a selected property with its corresponding boolean value as default to each element of valueOptions array and then reset this property as well when you reset the formControl 's value. 因此,请尝试将一个selected属性及其对应的boolean值作为默认值添加到valueOptions数组的每个元素中,然后在重置formControl的值时也重置此属性。

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

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