AngularJS的ng-switch-trigger在它自己的ng-switch-wrapper中

AngularJS ng-switch-trigger within its own ng-switch-wrapper

本文关键字:自己的 ng-switch-wrapper 它自己 ng-switch-trigger AngularJS      更新时间:2023-09-26

我正在尝试切换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/

用对象代替:

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中的原型继承是如何工作的。

在这里可以找到关于这个主题的很好的帖子。