0

我正在尝试切换 2 个按钮,它们是它们自己的开关触发器。为了更好地理解:

<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>

这是一些 jsFiddle:http: //jsfiddle.net/g7vKz/

4

1 回答 1

1

改用一个对象:

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

和:

<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>

演示:http: //jsfiddle.net/M7EX2/

ngSwitch创建一个新范围,这意味着除非您使用对象,否则由于原型继承在 JavaScript 中的工作方式,您将遇到此问题。

可以在此处找到有关该主题的非常好的帖子。

于 2014-05-13T08:39:32.333 回答