从控制器设置表单的值

Set the value of a form from the controller

我有 categories,其中有很多 products 和一个显示两者下拉菜单的表单。选择 Category 后,Product 下拉列表的值将被过滤,以便仅显示属于 categoryproducts。在用户将 selectedProduct 下拉菜单设置为特定值之前,它工作正常。

如果用户在单击 Product?

app/templates/index.hbs

{{view "select" prompt="All Categories"
                content=filteredCategories
                optionLabelPath="content.name"
                optionValuePath="content.name"
                value=selectedCategory}}

{{view "select" prompt="All Products"
                content=filteredProducts
                optionLabelPath="content.name"
                optionValuePath="content.name"
                value=selectedProduct}}

在索引控制器中 selectedCategory 的观察者上,您需要有一个条件来查看 selectedProduct 是否已经存在。如果是,则将 filteredProducts 设置为所有产品,并将 selectedProduct 设置为 '':

if (this.get('selectedProduct')) {
  this.set('filteredProducts', [<array of all products>]) && this.set('selectedProduct', '');
}

但是,我有点困惑为什么要在选择新类别时将其设置为所有产品的数组..你不想将 filteredProducts 设置为一个数组吗基于新类别的产品数量?