1

I have a div and in normal case display:block css is applied to it.And a hide button is there to hide the div on click on that button.But jquery's

$('#divid').css()

cant override css propoerty.Here is an example fiddle. jsfiddle

<div ng-controller="MyController">
   <div id="div1" class="showdiv">This is the div need to hide on click
   <a href='#' ng-click="hidedivfn()">Hide</a>
   </div>
   </div>
</div>

function MyController($scope) {
  $scope.hidedivfn=function(){
  $('#div1').css({ 'display': "none!important" });
  }

}

.showdiv{
  display:block;
}
akhil viswam
  • 496
  • 9
  • 24

4 Answers4

1

Don't use jquery in angularjs controller. Don't use hash in anchor. It will try to navigate. Use button. Please read more basics. Try like following,

  <div ng-controller="MyController">
       <div id="div1" ng-if="div.show">This is the div need to hide on click
       <a ng-click="hidedivfn()">Hide</a>
       </div>
       </div>
    </div>

    function MyController($scope) {
$scope.div={show:true};
      $scope.hidedivfn=function(){
      $scope.div.show=false;
      }

    }
Jeevanandan J
  • 144
  • 1
  • 8
0

To hide div ( or any element) use $('#div1').hide() And to show div (or any element) use $('#div1').show()

Ujwal Khairnar
  • 508
  • 1
  • 4
  • 19
0

you can use ng-hide for this purpose , look at snippet given below.

var app = angular.module("myApp",[]);

app.controller("MyController" , function($scope){
  $scope.hideDiv = false;
  
  $scope.hidedivfn = function(){
    $scope.hideDiv = true;
  }
  
   $scope.showdivfn = function(){
    $scope.hideDiv = false;
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyController">
   <div id="div1" ng-hide="hideDiv">This is the div need to hide on click
   
   </div>
  
  <a href='#' ng-click="hidedivfn()">Hide</a>
   <a href='#' ng-click="showdivfn()">show</a>
   </div>
</div>
Dinesh Shah
  • 1,163
  • 1
  • 7
  • 13
0

In Your case you don't have to override but you can achieve it with angular way

  1. ng-Show
  2. ng-if
  3. ng-hide
  4. ng-class

But among above you can use ng-show

<div id="div1" ng-show="stat">This is the div need to hide on click
<a ng-click="hidedivfn()">Hide</a>


$scope.stat = true;
function hidedivfn(){
    if($scope.stat == true){
        $scope.stat = false;
    }else{
        $scope.stat = true;
    }
}

Documentation for ng-showhere.

Regards

Hardik Vaghani
  • 2,163
  • 24
  • 46