I am trying to switch 2 buttons which are their own switch-triggers. For better understanding:

<span ng-switch on="patientSwitch">
    <span ng-switch-when="edit">
        <button ng-click="patientSwitch='save'">save</button>
    </span>
    <span ng-switch-when="save">
        <button ng-click="patientSwitch"='edit'">edit</button>
    </span>
</span>

Here is some jsFiddle: http://jsfiddle.net/g7vKz/

有帮助吗?

解决方案

Use an object instead:

myApp.controller("PatientCtrl", function($scope) {
  $scope.viewModel = { patientSwitch: "edit" };
});

And:

<span ng-switch on="viewModel.patientSwitch">
    <span ng-switch-when="edit">
        <button ng-click="viewModel.patientSwitch='save'">save</button>
    </span>
    <span ng-switch-when="save">
         <button ng-click="viewModel.patientSwitch='edit'">edit</button>
    </span>
</span>

Demo: http://jsfiddle.net/M7EX2/

ngSwitch creates a new scope, which means unless you use an object you will get this problem due to how prototypal inheritance works in JavaScript.

Very good post on the subject can be found here.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top