简体   繁体   English

当我在console.log中选择用户的单选按钮时,总是落后于哪些日志

[英]When I console.log a user's radio button selection, what logs is always one behind

For some reason, when I console.log a user's radio button selection, what logs to the console is always one behind. 出于某种原因,当我在console.log中选择一个用户的单选按钮时,对控制台的日志总是落后。 Why would this be? 为什么会这样? I have a default radio button selection set to "all" for the initial state. 我有一个默认的单选按钮选择设置为初始状态的“全部”。 When a user click's "Option A" what logs to the console is the "all" from the initial state. 当用户单击“选项A”时,登录到控制台的是从初始状态开始的“全部”。 When I click again, this time on "Option B", what logs is "Option A" from the previous selection. 当我再次点击时,这次在“选项B”上,什么日志是前一个选择中的“选项A”。 Why is it one off? 为什么一次性? How can I resolve this? 我该如何解决这个问题?

   public user: User;

  public categories = [
    { value: 'T', display: 'All', count: 232 },
    { value: 'A', display: 'Choice A', count: 22 },
    { value: 'B', display: 'Choice B', count: 43 },
    { value: 'C', display: 'Choice C', count: 35 },
    { value: 'D', display: 'Choice D', count: 62 },
    ];

  ngOnInit() {
    this.user = {
      category: this.categories[0].value
    }
  }

  select(isValid: boolean, f: User) {
    if (!isValid) return;
    console.log(f);
    console.log(this.user.category);
  }

And the HTML: 和HTML:

<form #f="ngForm" novalidate>
    <div class="form-group">
    <div class="radio" *ngFor="let category of categories">
        <label>
        <input type="radio" name="category" [(ngModel)]="user.category" [value]="category.value" (click)="select(f.value, f.valid)">
        {{category.display}}
        </label>
        <span class="num-results">{{category.count}}</span>
    </div>
    </div>
</form>

Use ngModelChange instead of click 使用ngModelChange而不是click

(ngModelChange)="select(f.value, f.valid)"

The (click) event is processed before ngModel had the chance to update the form. ngModel有机会更新表单之前处理(click)事件。

The order of bindings on an element doesn't define in what order events are processed. 元素上的绑定顺序不定义事件处理的顺序。

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

相关问题 当我在console.log中调用console.log时会发生什么? - What is happening when I call console.log within console.log? 什么是控制台.log? - What is console.log? 如何从ionic的console.log中删除日志? - How do I remove logs from console.log in ionic? console.dir 和 console.log 有什么区别? - What's the difference between console.dir and console.log? <button>单击按钮时,如何在 console.log 中显示特定文本?</button> - How do I make a <button> show a specific text in console.log when I click on the button? Object 是未定义的,但是当我 console.log 他们总是“有效” Node.js 表达 - Object is undefined, but when I console.log them always "works" Node.js express 我希望我的提交按钮以 console.log 将用户输入的所有值输入到一个数组中 - I want my submit button to console.log all the values input by the user in an array 为什么 console.log([1, 2, 3, 4, 5, 6, 7, 8, 9, 10][1, 2, 3, 4, 5, 6, 7][1, 2, 3]) 日志不明确的? - Why console.log([1, 2, 3, 4, 5, 6, 7, 8, 9, 10][1, 2, 3, 4, 5, 6, 7][1, 2, 3]) logs undefined? console.log如何在后台工作? - How console.log works behind the scene? 从两个不同的 Worker 线程调用的 console.log() 可以互相踩踏,还是它们总是一个接一个? - Can console.log()s called from two different Worker threads ever step on each other, or are they always one after the other?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM