无法在Angular js中设置属性错误

时间:2013-11-25 08:51:24

标签: javascript jquery angularjs

以下代码显示无法在angular js

中设置undefined属性'show'

HTML代码

<div ng-controller="DeathRayMenuController">
<button ng-click="toggleMenu()">Toggle</button>
<ul ng-show="menuState.show">
<li ng-click="stun()">Stun</li>
<li ng-click="disintegrate()">Disintegrate</li>
<li ng-click="destroy()">Destroy</li>
</ul>

Javascript:

   function DeathRayMenuController($scope) {
     $scope.menuState.show = false;

     $scope.toggleMenu = function() {
         $scope.menuState.show = true;
     }
}

在运行此操作时遇到此错误

TypeError:无法设置未定义属性'show'

1 个答案:

答案 0 :(得分:3)

必须首先定义

menuState

$scope.menuState = {
    show : false
};