如何在没有ng-model的情况下以角度2形式以编程方式更改原始属性 [英] How to programmatically change pristine property in angular 2 form without ng-model

查看:54
本文介绍了如何在没有ng-model的情况下以角度2形式以编程方式更改原始属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Angular 2框架的新手。我感谢任何帮助。

I'm new in Angular 2 framework. I appreciate any help.

我在角度2中有通常的组件:

I have usual component in angular 2:

import {FORM_DIRECTIVES, FormBuilder, Validators} from 'angular2/common';

export class TestComponent {
    public values = ['value1', 'value2', 'value3', 'value4'];
}

然后我注入 FormBuilder 进入构造函数:

Then I'm injecting FormBuilder into the constructor function:

@Inject(FormBuilder) public fb

HTML包含下一个标记:

HTML contain next markup:

<input [(ngModel)]="formData.title" type="text" class="form-control" ngControl="title">

标题和说明效果很好。
但是我添加了bootstrap下拉列表和它没有任何表单元素

Title and description works great. But I've added bootstrap dropdown and it has no any form element.

<div *ngFor="#value of values" (click)="onValueChanged(value)" class="dropdown-item">{{value}}</div>

因此,问题是html标记不包含任何模型。
我试图解决这个问题的方法是创建函数 onValueChanged

So, the problem is that html markup doesn't contain any model. The way I tried to solve this issue is to create function onValueChanged

 onValueChanged(value){
    this.formData.controls.value.updateValue(value);
    this.formData.value = value;
    console.log(this.formData.pristine) //Still true :(
}

这两行都不起作用,因为下拉列表更改后 this.formData.pristine 没有变化。

Both of these line are not working, because this.formData.pristine is not changes after dropdown is changed.

目前我正在考虑如何更新 FormBuilder ,可以使用一些方法,例如 this.fb.update ()

For now I'm thinking how to update FormBuilder, it would be fine to have some methods, for example this.fb.update()

推荐答案

你可以删除 pristine 状态使用

this.formData.controls.value.markAsDirty();

目前的可能性非常有限。另见 https://github.com/angular/angular/issues/4933

The current possibilities are quite limited. See also https://github.com/angular/angular/issues/4933

这篇关于如何在没有ng-model的情况下以角度2形式以编程方式更改原始属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆