根据班级ng-switch

时间:2016-04-21 15:44:53

标签: 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做到这一点,但我不知道如何做。

你能帮我吗?

3 个答案:

答案 0 :(得分:0)

我想你可以用这个:

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

答案 1 :(得分:0)

更短的版本:

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

我希望它有所帮助。

答案 2 :(得分:0)

试试这个希望可行吗

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