0

I want to run a function based on a $scope value. In my controller, I have:

 $scope.value = "";
 $scope.selector = function(info) { 
    if ($scope.value === "") {
       $scope.value = info;
    } else {
       $scope.value = "";
    }
 }

The function is triggered on ng-click of different images, e.g.:

<img ng-src="..." ng-click="selector(info)">

My problem is the if-operations dont seem to work on the $scope value. I have also tried:

 $scope.$watch('value', function(val) {
    if(val === "") {
       $scope.value = info;
    }
 }

This works if it is outside a function, but not if I place it in the selector function. Better approaches always welcome. Thanks for any explanations.

7
  • Your function expects a parameter, you don't give it one. Commented Jan 4, 2017 at 16:30
  • This has been a typo, I m doing that already, I corrected that. The parameter is in the code. My problem is that he if function does not detect the $scope.value === "" Commented Jan 4, 2017 at 16:32
  • What is info in ng-click="selector(info)"? Commented Jan 4, 2017 at 16:38
  • A string that I get from a string array: e.g. imageName[0], which should be "Image 1" Commented Jan 4, 2017 at 16:41
  • It seems like if I run the function directly, the if function runs. But not if I assign the function to a $scope variable. Commented Jan 4, 2017 at 17:09

2 Answers 2

1

Try to enclose your value inside an object. There are some cases where Angular cannot "see" that the variable changed.

So, try to keep something like:

$scope.value = { value: "" };

and change the rest of the code accordingly.

I did a codepen with some fixes in your code and with what I said to you: Running example

In this example you can see the val === "" returning true and false.

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

2 Comments

This did not solve it. The the if statement still results in false.
hi @sandboxj , I edited my answer with the if statement fixed.
1

You need to pass the parameter from the view to the function, because your function needs an input parameter

Change the view as,

img ng-src="..." ng-click="selector(value)">

9 Comments

Sorry, this has been a typo, I m doing that already, I corrected that. My problem is that he if function does not detect the $scope.value === "".
check if your function is getting called and the value being passed
I checked the value. I can console.log(info) and it shows up. But not if I place it inside the if function. Only if I place it before it.
No, unfortunately I do not have any account for screensharing. I understand that if operations should normally be possible on $scope variables then?
@sandboxj yes it should work, probably you can debug and check if the function is getting called and print the value
|

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.