Prakasam
Prakasam

Reputation: 9

ng-show, ng-hide not working outside the controller

i have tow div's. one is add and another one is view. any one div shows at a time. my add button is placed out of the controller.

     <input id="Button1" type="button" value="Add" onclick="AddClick()" />
 <div ng-app="myApp" ng-controller="csrControl" ng-init="viewdiv=true">
     <div id="view" ng-show="viewdiv">
         view
     </div>
     <div id="add" ng-hide="viewdiv">
        Add
     </div>
 </div>

Here i have externally called the controller and assign scope variable to false. the variable set as false, but the div cannot hide and show. ng-show and ng-hide not working

function AddClick() {
       var $scope = 'div[ng-controller="csrControl"]';
       $scope.viewdiv = 'false';
       $scope.$apply();
    }

Upvotes: 0

Views: 59

Answers (1)

holydragon
holydragon

Reputation: 6728

Here are the things I changed to get it working:

  • Put ng-app="myApp" ng-controller="csrControl" into the body.
  • Move ng-init="viewdiv=true" to $scope.viewdiv = true;.

and many little things. Check it out.

angular.module('myApp', ['myApp.controllers']);

angular.module('myApp.controllers', []).controller('csrControl', [
  '$scope', function($scope){
    $scope.viewdiv = true;
    $scope.addClick = function() {
       $scope.viewdiv = false;
    }
  }
]);
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="csrControl">
<input id="Button1" type="button" value="Add" ng-click="addClick()" />
 <div>
     <div id="view" ng-show="viewdiv">
         view
     </div>
     <div id="add" ng-hide="viewdiv">
        Add
     </div>
 </div>
</body>
</html>

Upvotes: 2

Related Questions