3

I'm trying to implement functionality for checking multiple items in a list of checkboxes. I am using a Map to store the data and its selected/unselected state.

List<String> fruits = [
  {'fruit': 'apple', 'selected': true},
  {'fruit': 'banana', 'selected': true},
  {'fruit': 'kiwi', 'selected': false}
];

The checkboxes display initially with the checked/unchecked status correctly rendered, but when I toggle a checkbox's checked status, the fruits map does not update. How can I correctly create a two-way binding here?

Here is my .dart file:

import 'package:angular/angular.dart';

@NgDirective(
  selector: '[my-controller]',
  publishAs: 'ctrl'
)
class MyController {
  List<String> fruits = [
    {'fruit': 'apple', 'selected': true},
    {'fruit': 'banana', 'selected': true},
    {'fruit': 'kiwi', 'selected': false}
  ];
}

main() {
  ngBootstrap(module: new Module()..type(MyController));
}

And here is the view markup:

<!DOCTYPE html>
<html ng-app>
  <body>
    <div my-controller>            
      <ul>
        <li ng-repeat="item in ctrl.fruits">
          {{item['fruit']}}
          <input type="checkbox" ng-model="item['selected']" />
        </li>
      </ul>
    </div>

    <!-- THIS DOES NOT UPDATE WHEN THE CHECKBOXES ARE TOGGLED -->
    {{ctrl.fruits}}

    <script type="application/dart" src="main.dart"></script>
    <script type="text/javascript" src="packages/browser/dart.js"></script>
  </body>
</html>
Shailen Tuli
  • 13,815
  • 5
  • 40
  • 51
  • Some additional data: If I change the markup in the `
  • ` to `{{item['fruit']}}-{{item['selected']}}`, toggling the checkboxes clearly does change the `selected` field. But the `{{ctrl.fruits}}` map as a whole does not seem to update.
  • – Shailen Tuli Jan 07 '14 at 19:00
  • I added a periodic timer to the constructor that prints the map. The 'selected' value gets updated in the map when the checkbox is changed. Only `{{ctrl.fruits}}` is not updated. – Günter Zöchbauer Jan 07 '14 at 19:06