0

the following is code shows cannot set property 'show' of undefined in angular js

HTML code

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

While running this im getting this error

TypeError: Cannot set property 'show' of undefined

1 Answer 1

3

menuState must be defined first:

$scope.menuState = {
    show : false
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this. Seems so obvious now but working from a reputable book source, you wouldn't think they'd make such silly mistakes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.