Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

231
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!