Search code examples
angularjsangularjs-ng-click

Scope variable data not changing in AngularJS


I want to change scope vairable data by below code but it is not working. I am not getting any error but it is not working like expected.

$scope.secondcity1 = false;
$scope.hidecity1 = function() {
  alert(secondcity1);
  $scope.secondcity1 = false;
  $scope.city1 = ''; 
  alert(secondcity1);
}

I am using alert(secondcity1); this to show alert box but it is not showing anything,

<div ng-style="{'display':secondcity1 == false?'none':'block'">  
  <!-- some codes -->
  <button type="button" class="remove" ng-click="hidecity1()">-</button>
</div>

above code is also not working. I am expecting to hide the div but it is not hiding it.


Solution

  • Try this, in AngularJs to access objects you have to use $scope like $scope.object

    $scope.secondcity1 = false;
    $scope.hidecity1 = function() {
            alert($scope.secondcity1);
            $scope.secondcity1 = false;
            $scope.city1 = ''; 
            alert($scope.secondcity1);
        }