根据类别切换

  • 本文关键字: angularjs ng-switch
  • 更新时间 :
  • 英文 :


我没有完全得到ng-switch。这就是我想做的:我的身体有一个可以根据我点击的按钮改变的类。

<body class="green">
      ....
<button onclick="document.body.className='green'">green</button>
<button onclick="document.body.className='blue'">blue</button>

想法是我需要通过 ng-include 包含一个文本,我希望它是

<div ng-include="green.html"></div> //if body has class green
<div ng-include="blue.html"></div> //if body has class blue

我知道我可以用ng-switch做到这一点,但我不知道怎么做。

你能帮帮我吗?

我认为你可以使用它:

<button ng-click="updateBodyClass('green')">green</button>
<button ng-click="updateBodyClass('blue')">blue</button>
<div ng-switch on="bodyClass">
    <div ng-include="green.html" ng-switch-when="green"></div> //if body has class green
    <div ng-include="blue.html" ng-switch-when="blue"></div> //if body has class blue
</div>

//inside your controller
$scope.updateBodyClass = function(color) {
    document.body.className = color;
    $scope.bodyClass = color;
};

试试这个希望这可以工作

<button ng-click="color='green'">green</button>
<button ng-click="color='blue'">blue</button>
<div>
    <div ng-include="green.html" ng-if="color==green"></div> //if body has class green
    <div ng-include="blue.html" ng-if="color==blue"></div> //if body has class blue
</div>

较短的版本:

<button ng-click="flag='green'">green</button>
<button ng-click="flag='blue'">blue</button>
<div ng-switch on="flag">
    <div ng-include="green.html" ng-switch-when="green"></div> 
    <div ng-include="blue.html" ng-switch-when="blue"></div> 
</div>

我希望它有所帮助。

相关内容

  • 没有找到相关文章

最新更新