0

I am trying to get and set a boolean to 'checkvalue'. I want beforehand to call a function isItTrue to set the variable 'checkvalue'. If the box is (un)checked/ and I check it, it should call a function doSomething(). I was experimenting with <input type="checkbox" id="{{ class.id }}" ng-init="checkBoxValue=isItTrue(class.id)"[(ngModel)]="class['checkBoxValue']"/> but it doesnt seem to work. Any suggestions?

    <table>
      <thead>
      <tr>
        <th>class</th>
        <th>subjects</th>
        <th>class speaker</th>
      </tr>
      </thead>
      <tbody>
      <tr *ngFor="let class of classes">
        <td>{{ class.name }}</td>
        <td>
          <div class="form">
            <input type="text" class="md-input-text" placeholder=" " value=" " [(ngModel)]="subject"/>
            <label class="md-desc">subjects</label>
          </div>
        </td>
        <td>
          <div class="md-checkbox">
            <input type="checkbox" id="{{ class.id }}" [(ngModel)]="class['checkBoxValue']"/>
            <label for="{{ class.id }}"></label>
          </div>
        </td>
      </tr>
      </tbody>
    </table>
Kostas Siabanis
  • 2,989
  • 2
  • 20
  • 22
stefPan
  • 17
  • 7

1 Answers1

0

To do something on the component initialization:

import { OnInit } from '@angular/core';

And make your component implement it like

export class App implements OnInit{ 
}

and you can call your method from the ngOnInit lifecycle hook method:

ngOnInit() {
    isItTrue();
}

Setting a boolean to checkvalue: You should have a component variable and bind it to the input with:

[(ngModel)]="checkvalue"

To call your function when the input changes, you can achieve it with the change event:

(change)="doSomething()"

In this function, you can access the value of the input with this.checkvalue which you already binded.

Seee this planker for the whole code. Hope it helps :).

padr
  • 385
  • 1
  • 4
  • 16