Angular JS - custom directive with ul-li elements as radio buttons and checkboxes

326 Views Asked by At

This is a small piece of code which has been written by me and thought it may help some one who are looking for similar functionality:

custom directives on li elements - clicking on which they select/unselect the li element and yields the selected objects in an array (can be collected under controller)

    <div ng-app="myApp" ng-controller="myCtrl">

  <ul>
    <li>
      <input type="text" ng-model="search_ms.title" />
    </li>
    <li 
    ng-repeat="x in items | filter:search_ms" 
    multi-select multi-select-item="x" 
    ng-class="{selected:x.checked}" 
    ng-click="getAllSelectedItems(items)">
      {{x.title}} --> {{x.checked}}
    </li>
  </ul>

  <ul>
    <li>
      <input type="text" ng-model="search_ss.title" />
    </li>
    <li 
    ng-repeat="x in items_ | filter:search_ss" 
    single-select 
    single-select-item="x" 
    unselect-all-items="items_" 
    ng-class="{selected:x.checked}" 
    ng-click="getAllSelectedItems(items_)">
      {{x.title}} --> {{x.checked}}
    </li>
  </ul>

<div class="clearfloat">
</div>


  <br/>
  <br/>
  <br/>

  <p>
  {{selectedItems}}
  </p>

  <br/>
  <br/>
  <br/>

  <p>
  {{unselectedItems}}
  </p>

JS Fiddle: http://jsfiddle.net/AjayBunga/NBhn4/230/

Both single select (like radios) and multi select (like checkboxes) has been implemented. Please suggest if I can optimise any code or if can be done in much simpler way as I am new to angular directives.

Thanks in advance.

1

There are 1 best solutions below

0
On BEST ANSWER

instead of using ng-click and capturing the changes with a timeout

this answer helped me in writing a dircetive callback function

AngularJS Directive Callback