0

I am new to this, I want to change the value of a variable when the button is fired. I have an array selectedValues, which contains a status of a project i.e Ready/NotReady.

What I want to do is to change the status to Not Ready if the status is Ready and vice versa.

I want a js function to do this. I was wondering if I could do it like this?

 $scope.changeStatus = function(selectedValue){
    if(selectedValue.status == "Not_Ready")
    selectedValue.status = "Ready"
}
5
  • yes you can do this. what is the problem Commented Jun 22, 2017 at 8:27
  • 1
    You aren't changing the value of a variable, you're assigning a value to a property of an object (Arrays are Objects). Commented Jun 22, 2017 at 8:29
  • @RobG so how can i perform it? Commented Jun 22, 2017 at 8:55
  • I would suggest. use boolean if there are only two cases. then it will become more easier. true for ready and false if not ready Commented Jun 22, 2017 at 9:20
  • can you post a plunker? Commented Jun 22, 2017 at 9:21

3 Answers 3

1
$scope.changeStatus = function(selectedValue){
    selectedValue.status == "Not_Ready" ? selectedValue.status = "Ready" : selectedValue.status = "Not_Ready"
}

Try this

Sign up to request clarification or add additional context in comments.

Comments

1

If you ever add more statuses that you'd like to cycle through, you can do something like this:

$scope.changeStatus = function(selectedValue) {
    var statuses = ['State_A', 'State_B', 'State_C', 'State_D', 'State_E'];

    selectedValue.status = statuses[(statuses.indexOf(selectedValue.status) + 1) % statuses.length];
};

Comments

0

You can do it like this as well :

$scope.changeStatus = function(){
    if($scope.selectedValue.status == "Not_Ready"){
        $scope.selectedValue.status = "Ready"
    }else{
        $scope.selectedValue.status = "Not_Ready"
    }
}

5 Comments

you just added else block in my way of doing. nothing else.
Yes, and you'll need to as $scope before selected value as well.
you mean $scope.selectedValue?
Yes. Only selectedValue will be limited to function, but $scope.selectedValue will be in whole controller
in the parameter ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.