1

I have a slider that can change volume. I also have a button that raises the count variable by 1 every time it's clicked.

I wrote an if-statement which should give an alert if those two variables match but it's not working.

if ($scope.count == $scope.data.volume) {
   alert('finished')
}

Codepen example

What's going wrong here?

2 Answers 2

2

your If statement it's out of the plusOne function

$scope.plusOne = function() {
    $scope.count++
}
if ($scope.count == $scope.data.volume) {
    alert('finished')
}

it will not work until you include

$scope.plusOne = function() {
   $scope.count++
   if ($scope.count == $scope.data.volume) {
      alert('finished')
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't account for the fact that the range slider could change the value of data.volume
0

Looking at the code you posted on codepen (posted here, because the code you posted in your question doesn't provide enough context);

angular.module('ionicApp', ['ionic'])
  .controller('MyCtrl', function($scope, $timeout) {
    $scope.myTitle = 'Template';
    $scope.data = { 'volume' : '5' };
    $scope.count = 0;
    $scope.plusOne = function() {
      $scope.count++
    }
    if ($scope.count == $scope.data.volume) {
      alert('finished')
    } 
  });

You are only checking if $scope.count == $scope.data.volume once, when the controller is instantiated. Your code doesn't account for the fact that you need to do the check when the values actually change too. You'll need to use $scope.$watchCollection.

If using Angular > 1.2 (and it looks like you are from your codepen)...

$scope.$watchCollection('[count,data.volume]',function() {
  if ($scope.count == $scope.data.volume) {
    alert('finished')
  }
});

Note, that first parameter to $scope.$watchCollection is not an array - it's a string that sort of looks like an array.

Comments

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.