AngularJS ng-switch在<p>标签</p>上引发错误

时间:2014-09-18 05:50:05

标签: javascript html angularjs dom

我对AngularJS很新,刚刚开始使用它,我遇到ng-switch,我发现这只适用于<div>标记,例如

<div ng-app="myApp" ng-controller="switchController">
    <div ng-switch on="myData.text">
      <p ng-switch-when="1">Switch To 1</p>
      <p ng-switch-when="2">Switch To 2</p>
      <p ng-switch-default>Switch To Default</p>
    </div>
</div>

当我在下面声明它时会抛出错误!

<div ng-app="myApp" ng-controller="switchController">
    <p ng-switch on="myData.text">
      <p ng-switch-when="1">Switch To 1</p>
      <p ng-switch-when="2">Switch To 2</p>
      <p ng-switch-default>Switch To Default</p>
    </p>
</div>

JavaScript:

angular.module("myApp", [])
        .controller("switchController", function($scope) {
            $scope.myData = {};
            $scope.myData.text = 2;
            console.log($scope.myData.text);
});

PLUNKER DEMO

1 个答案:

答案 0 :(得分:2)

抱歉,我的评论太早发布了。

这不起作用,因为嵌套的<p>标记是无效的HTML。

<div ng-app="myApp" ng-controller="switchController">
    <p ng-switch on="myData.text">
      <p ng-switch-when="1">Switch To 1</p>
      <p ng-switch-when="2">Switch To 2</p>
      <p ng-switch-default>Switch To Default</p>
    </p>
</div>