【问题标题】:mdl-radio with ngFor each radio button that is clicked remains selected带有 ngFor 的 mdl-radio 每个单击的单选按钮保持选中状态
【发布时间】:2017-12-05 12:12:08
【问题描述】:

我有三个产品数组和一个变量来保存当前的一个作为选择:

product_types=['One', 'Two', 'Three']

product_type_one = [
{'description': 'Type one sample one', 'type': 'one'},
{'description': 'Type one sample two', 'type': 'one'},
{'description': 'Type one sample three', 'type': 'one'},
] 

product_type_two = [
{'description': 'Type two sample one', 'type': 'two'},
{'description': 'Type two sample two', 'type': 'two'},
{'description': 'Type two sample three', 'type': 'two'},
] 

product_type_three = [
{'description': 'Type three sample one', 'type': 'three'},
{'description': 'Type three sample two', 'type': 'three'},
{'description': 'Type three sample three', 'type': 'three'},
] 

selectedProduct=null;

在我的模板上,我正在填充单选按钮,以便用户可以选择以下产品组之一:

  <div class="my-filters" *ngFor="let product of product_types">
    <mdl-radio 
    name="productgroup" 
    value="product" 
    ngModel='selectedProduct'
    (change)="showProduct(product)"
    mdl-ripple>{{product}}</mdl-radio>
  </div><br>


 <mdl-card *ngFor="let product of selectedProduct" class="demo-card-event" mdl-shadow="2">
  <mdl-card-title mdl-card-expand>
   <h4>
     {{product.description}}
   </h4>
 </mdl-card-title>
 <mdl-card-actions mdl-card-border>
  <button mdl-button mdl-colored mdl-ripple (click)="openDialog()">
    view
  </button>
  <mdl-layout-spacer></mdl-layout-spacer>
  <mdl-icon>shopping_cart</mdl-icon>
</mdl-card-actions>
</mdl-card>

直到这里,无论用户单击什么单选按钮,我的 mdl 卡中都会填充正确的数组,但是用户一个接一个地单击的所有单选按钮仍然被单击,而不仅仅是当前选择:

我做错了什么?

【问题讨论】:

    标签: angular angular2-mdl angular-mdl


    【解决方案1】:

    您应该使用md-radio-group 对它们进行分组,如下所示,

    <md-radio-group class="productgroup" [(ngModel)]="selectedProduct">
       <mdl-radio  *ngFor="let product of product_types"
           value="product" 
          (change)="showProduct(product)"
          mdl-ripple>{{product}}</mdl-radio>
    </md-radio-group>
    

    【讨论】:

    • 首先谢谢! md-radio-group 是否有错字,即 mdl 而不是 md?此外,如果我将它添加到 mdl-radio-group,它会给我一个错误,说它不是已知元素
    • 您使用的是什么版本的材料设计?
    【解决方案2】:

    我更新了我的 for 循环,如下所示:

     <div *ngFor="let product of product_types" class="my-filters">
        <mdl-radio name="productgroup" [value]='product' [(ngModel)]="selectedProduct" (change)="showProduct(product)"> {{product}} </mdl-radio> <br>
      </div>
    

    【讨论】:

      猜你喜欢
      • 2021-07-08
      • 2017-03-16
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多