简体   繁体   English

角度-手动更改ngModel的值不会将其检测为有效

[英]Angular - changing ngModel value manually doesn't detect the value as valid

I'm changing the value of a select in my template: 我正在更改模板中的选择值:

        <select id="CollaboratorId" 
                    name="CollaboratorId"
                    formControlName="CollaboratorId"
                    class="form-control col-sm-8" 
                    [(ngModel)]="SelectedUser.CollaboratorId" 
                    (change)="onUserChange($event.target.value)" required>
                <option [ngValue]="undefined">Select Collaborator</option>
                <option *ngFor="let collaborator of CollaboratorsList" [ngValue]="collaborator.collaboratorId">{{collaborator.collaboratorFullName}}</option>
        </select>

From my component with its ngModel name like this: 从我的组件中,其ngModel名称如下所示:

this.SelectedUser.CollaboratorId = data.collaboratorId;

When running my program I can see indeed the value has changed, however because the value wasn't selected on change it's not being registered as valid. 运行程序时,我确实可以看到该值已更改,但是由于更改时未选择该值,因此未注册为有效值。 I know there's no way to change a form's control to valid, so is there a method to instead trigger the (change)? 我知道没有办法将窗体的控件更改为有效控件,因此是否有方法代替触发(更改)? Thanks in advance. 提前致谢。

I think it is not working properly because you are mixing reactive and template driven forms. 我认为它不能正常工作,因为您混合了反应形式和模板驱动形式。 Try removing [(ngModel)]. 尝试删除[(ngModel)]。

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

相关问题 当`ngModelChange`保持值时,Angular ngModel不会更新 - Angular ngModel doesn't update when `ngModelChange` keeps value Angular 8 Select with [(ngModel)] 设置值,但文本不显示在选项中 - Angular 8 Select with [(ngModel)] sets the value but the text doesn't display in the option Angular - 使用 ngModel 和 ngModelChange 手动限制输入的最大值 - Angular - Manually restricting max value of input using ngModel and ngModelChange Angular 6 ngModel不会更改appcomponent中的值 - Angular 6 ngModel didn't change value in appcomponent Angular 4:NgModel不起作用。不将值设置为“日期”字段 - Angular 4: NgModel doesn't work. Does not set value to “date” field 如何在 Angular 中获取输入值( element.setAttribute(&quot;[(ngModel)]&quot;, &quot;Title&quot; 不起作用) - How to get value of input in Angular ( element.setAttribute("[(ngModel)]", "Title" doesn't work) Angular 材料 mat-radio-group 不允许使用 ngModel 的默认值 - Angular material mat-radio-group doesn´t allow default value with ngModel 用Javascript更改输入值后如何强制Angular 2 [(ngModel)]更改? - How to force Angular 2 [(ngModel)] change after changing input value with Javascript? 使用 [(NgModel)] 更改绑定的 Angular 表中的值会影响其他字段 - Changing the value in a binded Angular table with [(NgModel)] affects other fields Angular 2 NgModel不起作用 - Angular 2 NgModel doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM