Ben McCann
Ben McCann

Reputation: 19004

Building a toggle with ng-switch

I can't figure out why this code doesn't work as a toggle. Any ideas what I might be doing wrong?

  <div ng-switch="!!myvar">
    <a ng-switch-when="false" ng-click="myvar = true" style="cursor:pointer">
      false
    </a>
    <a ng-switch-default ng-click="myvar = false" style="cursor:pointer">
      true
    </a>
  </div>

I'm using AngularJS 1.2.0-rc.2.

Upvotes: 0

Views: 461

Answers (1)

zs2020
zs2020

Reputation: 54542

The directive ng-switch creates a new scope, so you need to use $parent to update the myvar in the parent scope.

<a ng-switch-when="false" ng-click="$parent.myvar = true" style="cursor:pointer">
<a ng-switch-default ng-click="$parent.myvar = false" style="cursor:pointer">

Upvotes: 3

Related Questions