2

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.

1
  • 1
    use $scope in alert Commented Apr 27, 2017 at 3:55

2 Answers 2

1

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);
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer. Let me try. Voted up.
well alert is working but divs are not getting hidden. Please advise.
try this <div ng-hide="!secondcity1"> <!-- some codes --> <button type="button" class="remove" ng-click="hidecity1()">-</button> </div>
1

Your alert should display the $scope variable, otherwise it will be undefined

Change

From

alert(secondcity1);

To

alert($scope.secondcity1);

2 Comments

Thanks for your answer. Let me try. Voted up.
well alert is working but divs are not getting hidden. Please advise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.