Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
checkbox doesn't work with ngfor in angular 2 and semantic UI

I'm using Angular2 and Semantic UI and I want to create a multiple selection component with checkbox like this https://jsfiddle.net/jp8xj0wk/2/, but when I iterate with *ngFor the checkbox doesn't work, but if I insert manually checkbox items only these work fine.

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'});
  }
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

this a good usecase for component lifecycle hooks, I'm going to add a button to your component to illustrate the solution. This new button will add a new element to the array, nothing more.

here is a working plunker: https://plnkr.co/edit/K5MWKzfCFjGmeOzYYxIJ?p=preview

the main thing here, is you add the array on or before ngOnInit and you initialize the checkbox and the select in AfterViewChecked

this way, every time there is a change (such as adding a new element to array). When Angular is done rendering the view it calls AfterViewChecked, where we init the select and 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();
  }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda