Angularjs ng-switch无法正常工作

时间:2014-03-19 09:23:41

标签: angularjs angular-ui

我想做的是:

单击下拉切换时,如果单击文本,则会显示“test_now”,如果单击Paragragh文本,则会显示“文本列表”。

但是我曾多次尝试但仍然无法正常工作,有人可以帮助我吗?非常感谢!

这是我的代码:

<li class="dropdown">
    <button class="btn btn-default dropdown-toggle">Add item</button>
    <ul class="dropdown-menu">
         <li ng-repeat="choice in items" ng-model="selection">
        <a>{{choice.type}}</a>
         </li>
    </ul>
</li>


<div ng-switch on "selection">
    <div class="question_typeText" ng-switch-when="Text">
        test_now
    </div>

    <div class="question_typeList" ng-switch-when="Paragraph Text">
        test_list
    </div>
</div> 

js part:

angular.module('DFS', ['ui.bootstrap']);

function PageCtrl($scope) {

    $scope.items = [
        {
            type: "Text"
        },
        {
            type: "Paragraph Text"
        },
        {
            type: "Multiple Choice"
        },
        {
            type: "Checkboxes"
        },
        {
            type: "Choose from a list"
        }
    ];

    $scope.selection = $scope.items[0].type;


}

1 个答案:

答案 0 :(得分:3)

这是我的示例代码:

    <div ng-controller="PageCtrl">
        <li class="dropdown">
          <button class="btn btn-default dropdown-toggle">Add item</button>
           <ul class="dropdown-menu">
             <li ng-repeat="choice in items">
               <a ng-click="click(choice)">{{choice.type}}</a>
             </li>
          </ul>
       </li>


      <div ng-switch on="selection">
         <div class="question_typeText" ng-switch-when="Text">
           test_now
         </div>

      <div class="question_typeList" ng-switch-when="Paragraph Text">
            test_list
      </div> 

      angular.module('myApp', []);
        function PageCtrl($scope) {

    $scope.items = [
        {
            type: "Text"
        },
        {
           type: "Paragraph Text"
        },
        {
        type: "Multiple Choice"
        },
        {
        type: "Checkboxes"
        },
        {
        type: "Choose from a list"
        }
    ];

    $scope.click = function(choice){
        $scope.selection =  choice.type;
    }

    $scope.selection = $scope.items[0].type;


}

或angularjs.org示例

http://docs.angularjs.org/api/ng/directive/ngSwitch