简体   繁体   English

Angular 下拉更改()与 valueChanges.subscribe

[英]Angular Dropdown Change() vs valueChanges.subscribe

I have an Angular form built using Reactive Forms.我有一个使用反应式 Forms 构建的 Angular 表单。 The form contains 3 select elements.该表单包含 3 个 select 元素。 I want to add the value change detection to one dropdown and other two will change accordingly to the values of the first dropdown.我想将值更改检测添加到一个下拉列表中,其他两个将根据第一个下拉列表的值进行相应更改。 Between Change() and valueChanges.subscribe , which will be the best to use and what is the difference.Change()valueChanges.subscribe之间,这将是最好用的,有什么区别。

They are fundamentally the same thing, each time the value of your dropdown changes, a function that change the value of the two other dropdowns is executed.它们本质上是一样的,每次下拉列表的值发生变化时,都会执行一个改变其他两个下拉列表值的 function。
valueChanges is an Observable, you have to unsubscribe from it at some point, for instance when the component is destroyed. valueChanges是一个 Observable,你必须在某些时候取消订阅它,例如当组件被销毁时。

it depends on your use case这取决于您的用例

  1. if your list contain an autocomplete or a realtime search to fill options i recommand the use valueChanges that returns an observable.如果您的列表包含自动完成或实时搜索来填充选项,我建议使用返回 observable 的 valueChanges。
  2. if it's a simple dropdown list use change event instead it's simple and no need to use valueChange and handle unsubscribes如果它是一个简单的下拉列表,请使用更改事件而不是它很简单,无需使用 valueChange 和处理取消订阅

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

相关问题 FormControl valueChanges.subscribe 与 keyup - FormControl valueChanges.subscribe vs keyup Angular2(beta)valueChanges.subscribe不在控制台中跟踪结果 - Angular2 (beta) valueChanges.subscribe not tracing result in console Angular 无功 forms:变化与值变化 - Angular Reactive forms : change vs valueChanges 如何知道在valueChanges.subscribe(…)内部是通过用户交互还是通过patchValue(…)来更改值 - How to know inside valueChanges.subscribe(…) if the value was changed by an user interaction or by patchValue(…) RangeError:使用 valueChanges.subscribe 时超出了最大调用堆栈大小 - RangeError: Maximum call stack size exceeded when using valueChanges.subscribe 在Angular2中对valueChanges进行空订阅 - Empty subscribe on valueChanges in Angular2 Angular2:在formbuilder中订阅值更改 - Angular2: Subscribe valuechanges in formbuilder Angular 订阅表单组中表单数组中的值更改 - Angular Subscribe valuechanges in formarray in form group Angular ReactiveFrom订阅valueChanges赋予[object Object] - Angular ReactiveFrom subscribe on valueChanges gives [object Object] Angular 2+:如何订阅白名单字段的valueValueChanges? - Angular 2+: how to subscribe to form valueChanges of whitelisted fields?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM