复选框不适用于角度2和语义UI中的ngfor

时间:2017-04-25 21:51:23

标签: angular checkbox semantic-ui ngfor

我正在使用Angular2和Semantic UI,我想创建一个带有此https://jsfiddle.net/jp8xj0wk/2/复选框的多选组件,但是当我使用* ng迭代时,复选框不起作用,但如果我手动插入复选框只有这些项目才能正常工作。

import { Component, OnInit, Input } from '@angular/core';

declare var $: any;

@Component({
  selector: 'combo-multiple',
  template: `
  <div class="ui basic right labeled dropdown icon button">
    <i class="dropdown icon"></i>
    <span class="ui tiny header">Items</span>
    <div class="menu">
      <div class="ui icon search input">
        <i class="search icon"></i>
        <input type="text" name="search" placeholder="Search...">
      </div>
      <div class="scrolling menu">
        <!-- Checkbox inserted manually works fine -->
        <div class="ui checkbox item" data-value="item -1">
          <input type="checkbox" name="item-1">
          <label>item-1</label>
        </div>
        <div class="ui checkbox item" data-value="item 0">
          <input type="checkbox" name="item0">
          <label>item0</label>
        </div>
        <!-- End. Checkbox inserted manually works fine -->


        <!-- checkbox with ngFor doesn't work -->
        <div class="ui checkbox item" *ngFor="let item of items" [attr.data-value]="item">
          <input type="checkbox" name="{{item}}">
          <label>{{item}}</label>
        </div>
      </div>
    </div>
  `
})
export class ComboMultiple implements OnInit {
  items: string[];
  constructor() {}

  ngOnInit() {
    this.items = ["Item 1","Item 2","Item 3"];
    $('.ui.checkbox').checkbox();
    $('.ui.dropdown').dropdown({action:'nothing'});
  }
}

1 个答案:

答案 0 :(得分:1)

这是组件lifecycle hooks的一个很好的用例,我将在您的组件中添加一个按钮来说明解决方案。这个新按钮将为数组添加一个新元素,仅此而已。

这是一个工作的掠夺者:https://plnkr.co/edit/K5MWKzfCFjGmeOzYYxIJ?p=preview

这里的主要内容是,你在 ngOnInit之前或之前添加数组,并初始化复选框和AfterViewChecked中的选择

这样,每次都有变化(例如向数组添加新元素)。当Angular完成呈现视图时,它调用AfterViewChecked,我们在其中启动select和checkbox。

@Component({
  selector: 'combo-multiple',
  template: `
  <div class="ui basic right labeled dropdown icon button">
    <i class="dropdown icon"></i>
    <span class="ui tiny header">Items</span>
    <div class="menu">
      <div class="ui icon search input">
        <i class="search icon"></i>
        <input type="text" name="search" placeholder="Search...">
      </div>
      <div class="scrolling menu">
        <!-- Checkbox inserted manually works fine -->
        <div class="ui checkbox item" data-value="item -1">
          <input type="checkbox" name="item-1">
          <label>item-1</label>
        </div>
        <div class="ui checkbox item" data-value="item 0">
          <input type="checkbox" name="item0">
          <label>item0</label>
        </div>
        <!-- End. Checkbox inserted manually works fine -->


        <!-- checkbox with ngFor doesn't work -->
        <div class="ui checkbox item" *ngFor="let item of items" [attr.data-value]="item">
          <input type="checkbox" name="{{item}}">
          <label>{{item}}</label>
        </div>
      </div>
    </div>
  </div>
    <!-- NEW BUTTON -->
    <button (click)="addItem()">add new</button>
  `
})
export class ComboMultiple implements OnInit, AfterViewInit, AfterViewChecked {
  items: string[];
  constructor() {}

  ngOnInit(){
    // add those three items on initialization of component.
    this.items = ["Item 1","Item 2","Item 3"];
  }

  // when called, adds a new item to the array
  addItem(){
    this.items.push("new item")
  }

  // when called, will initialize all dropdown and all checkboxes.
  initializeDropdown(){
    $('.ui.checkbox').checkbox();
    $('.ui.dropdown').dropdown({action:'nothing'});
  }
  // Called after the ngAfterViewInit and every subsequent ngAfterContentChecked().
  ngAfterViewChecked() {
    this.initializeDropdown();
  }
}