-1

I have a div which I would like to expand/contract when I click on it. I created the div:

<div ng-click="disclaimer();" style="height:100px;width:100px;overflow:{{expand}}">Sample Text</div>

When the user clicks the div, it simply toggles $scope.expand from hidden to default (or atlteast it should)

 $scope.disclaimer=function(){
  if($scope.expand="hidden"){
    $scope.expand="default";
  }
  else if($scope.expand="default"){
      $scope.expand="hidden";
  }
}

Right now, it expands the div (so $scope.expand changes from hidden to default) but does not contract when I click the div again. Any ideas? Thanks for the help

  • 7
    `=` is for assignment, `==` and `===` are for comparison. – Pointy Jun 09 '17 at 22:00
  • In other words, the first 'if' is always true. Because you'll always be assigning (=) instead of comparing (==). Hence the reason it will expand but not collapse. – hack3rfx Jun 09 '17 at 22:01

2 Answers2

1
 $scope.disclaimer=function(){
  if($scope.expand="hidden"){
    $scope.expand="default";
  }
  else if($scope.expand="default"){
      $scope.expand="hidden";
  }
}

to this

 $scope.disclaimer=function(){
  if($scope.expand=="hidden"){
    $scope.expand="default";
  }
  else if($scope.expand=="default"){
      $scope.expand="hidden";
  }
}

See here

jdmdevdotnet
  • 1
  • 2
  • 19
  • 50
0

Remember: in angularjs we can set functions in the view side too instead controller or ...

for example in your question you don't need to a function in controller to handle the view, for that you can use a lot of default directives as ng-style or ng-class

please run and see the sample

var app = angular.module("app", []);
.my-class {
  height:100px;
  width:400px;
  border: solid 1px #ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app">
  <button ng-click="expand = !expand" >click me</button>
  
   <b>expand is {{expand}} then overflow is {{expand ? 'auto':'hidden'}}</b>
   <br>
   <br>
  <div class="my-class"
    ng-style="{'overflow': expand ? 'auto':'hidden'}">
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
  </div>
  
 
</div>
Community
  • 1
  • 1
Maher
  • 2,517
  • 1
  • 19
  • 32