javascript - toggle dom elements with ng-if -


i toggle dom elements in dashboard using ng-if.

my html looks like:

//buttons

<div> <button ng-click="showdiv('div1');"></button> <button ng-click="showdiv('div2');"></button> </div> 

//content

<div ng-if="div1" class="div-1"></div> <div ng-if="div2" class="div-2"></div> 

//inside controller:

    $scope.div1 = true;     $scope.div2= false;     $scope.showdiv = function(element){        if($scope.element = 'div2'){          $scope.div2 = true;          $scope.div1 = false;        }       } 

actually value assignments div1 , div2 work initially, function not working properly.

i fiddling around while don't find solution...some great.

thanks, hucho

you should use element, parameter

 $scope.showdiv = function(element){        if(element === 'div2'){          $scope.div2 = true;          $scope.div1 = false;        } } 

demo

var app = angular.module('myapp', []);  app.controller('myctrl', function($scope) {     $scope.div1 = true;      $scope.div2= false;      $scope.showdiv = function(element){         if(element === 'div2'){           $scope.div2 = true;           $scope.div1 = false;         }      }  });
<!doctype html>  <html>  <script   src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js">  </script>  <body>  <div ng-app="myapp" ng-controller="myctrl">   <div>  <button ng-click="showdiv('div1');">div1</button>  <button ng-click="showdiv('div2');">div2</button>  </div>  <div ng-if="div1" class="div-1">test1</div>  <div ng-if="div2" class="div-2">test2</div>  </div>


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -