简体   繁体   English

如何在 Angular 8 中检测 angular 中的值是由用户更改还是方法调用已更改它

[英]How to detect in Angular 8 whether a value in angular is changed by user or a method call has changed it

I have a select element in which value is getting assigned by ngOnit call.我有一个 select 元素,其中的值由 ngOnit 调用分配。

HTML: HTML:

<select name="duration" [(ngModel)]="exercisePlan.duration" (ngModelChange)="onChange($event)"> 
    <option *ngFor="let duration of durations" [value]="duration.value">{{duration.title}}</option> 
</select> 

TS: TS:

durations =  [{ title: "15 seconds", value: 15 }, 
              { title: "30 seconds", value: 30 }, ...]

ngOnit() {
    this.exercisePlan.duration = durations[0];
}

onChange(event) {
    console.log("How to show that change is done by user but not by some method call");
}

So the value is not assigned by user and is made available by some method (ngOnInit, in this case).因此,该值不是由用户分配的,而是通过某种方法(在本例中为 ngOnInit)提供的。

Whereas when I select a value from dropdown then how to know whether user has made the selection from select options?而当我从下拉列表中选择一个值时,如何知道用户是否从选择选项中进行了选择?

Similar to that suppose if I add a Reset button in my HTML and on click of Reset I change value of my Select element, so how to distinguish in onChange() method whether the change is done by user or any other method call has made the changes to that element model?与假设类似,如果我在 HTML 中添加一个重置按钮并单击重置我更改了我的 Select 元素的值,那么如何在 onChange() 方法中区分更改是由用户完成还是由任何其他方法调用完成对该元素模型的更改?

Hi i would recommend to use FormControl.嗨,我建议使用 FormControl。 I would paste the correct code and then i would explain.我会粘贴正确的代码,然后我会解释。

ngOnit() {
      this.control.setValue(
        // value to set
        durations[0], 
        // this prevent the control from notify the change (optional)
        { emitEvent: false }
      );
    }

<select name="duration" [formControl]="control">
  <option value="">Select a value</option>
  <option *ngFor="let duration of durations" [ngValue]="duration.value">{{duration.title}}</option>
</select> 

on the component:在组件上:

valueDetection() {
this.formControl.valueChanges
      .subscribe(value => {
        // value is the value selected or assigned
        // here you can do logic
      });
}

In this example the FormControl change either when the user select a option or on code by a method.在此示例中,FormControl 会在用户选择选项或通过方法代码时更改。 The FormControl is a observable so it will be activate every time the value change and you can control logic associated with that change inside the subscribe. FormControl 是一个可观察对象,因此每次值更改时它都会被激活,您可以在订阅中控制与该更改关联的逻辑。 Now i would make 2 assumption here.现在我会在这里做 2 个假设。

First one is that you want to make code change without triggering the change.第一个是您希望在不触发更改的情况下更改代码。 That is archived by the values in the setValue method.这是由 setValue 方法中的值存档的。

Second you want to deal with logic depending if it is from code or not the change.其次,您要处理逻辑,这取决于它是否来自代码或不是更改。 This one is tricky.这个很棘手。 In order to know if the change is from code you must check the dirty and and touched values of the FormControl.为了知道更改是否来自代码,您必须检查 FormControl 的脏值和触摸值。 The second choice is to make an object like this: { value: 'the value', fromCode: true} and deal with the second value on the subscribe第二种选择是创建一个这样的对象:{ value: 'the value', fromCode: true} 并处理订阅上的第二个值

More info about FormControl有关FormControl 的更多信息

More info about Selects有关选择的更多信息

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

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