【问题标题】:AngularJS ng-switch-trigger within its own ng-switch-wrapperAngularJS ng-switch-trigger 在它自己的 ng-switch-wrapper 中
【发布时间】:2014-05-13 08:35:44
【问题描述】:

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

【问题讨论】:

    标签: javascript angularjs triggers wrapper ng-switch


    【解决方案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 中的工作方式,您将遇到此问题。

    关于这个主题的非常好的帖子可以在here找到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-24
      • 2013-03-13
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 2015-10-12
      相关资源
      最近更新 更多