1

I have a variable that I want to change when another scope changes its value.

$scope.switch = true;

var thing;
if ($scope.switch == false) {
    thing = "givesFalse";
}
else {
    thing = "givesTrue";
};
this.thingscope = thing;

So when I change $scope.switch value to false, this.thingscope should output givesFalse. In order to change the scope value, I use ng-click:

<div ng-controller="myCtrl as myCtrl" ng-app="myApp">
    {{myCtrl.thingscope}}
    <br>
    <a ng-click="switch = !switch">{{switch}}</a>   
</div>

But even that I can see that the scope does update, the variable thing doesn't seem to update at the same time. You can see the working plunkr here. Thanks in advance!

2 Answers 2

4

You have 2 options:

0 - Automatic: Using Watch

$scope.$watch('switch', function () {
  $scope.thing = $scope.switch ? "givesTrue" : "givesFalse";
});

1 - Manual: Use a custom function which change both values.

ng-click="switch = !switch"

change it to:

ng-click="customFunction()"

And define in the controller:

$scope.customFunction = function () {
  $scope.switch = !$scope.switch;
  $scope.thing = $scope.switch ? "givesTrue" : "givesFalse";
}
Sign up to request clarification or add additional context in comments.

10 Comments

I used the second one and it works perfect (plnkr.co/edit/FGovwUjEZtaRgkO85S0q?p=preview) Thanks for the approach!
Here is a http://plnkr.co/edit/1DvOpeRsyXEhmQzgE7Nl?p=preview based on your previous plnkr. With both implementations:
@joe82 - My recommendation is that you use the $scope.watch method, as that's what it's built for. Let Angular do the majority of the work for you.
@JoséLezama in the first implementation, how is it suppose to work? I cannot switch it in the plunkr by clicking the switch link
@Joe82 Check the Plunkr: plnkr.co/edit/v8VscDRUhVd0IG0bNYjq?p=preview Please use $scope instead this (is more Angular Way). I assume you are using lodash (or underscore, I prefer lodash so I include lodash over underscore). Plus, I didn't find the ng-lodash CDN but you must use for a more elegant code. github.com/rockabox/ng-lodash I use it without problem. I just: - Change this to $scope - Add the lodash lib.
|
1

The main point is that your Controller's code will be executed only once when your Controller is instanciated. If you want it to respond to changes, you'll need to use $watch.

$scope.$watch('switch', function(newSwitchValue, oldSwitchValue) {
  if ($scope.switch == false) {
    $scope.thing = "givesFalse";
  }
  else {
    $scope.thing = "givesTrue";
  }
});

In a real-world app where the architecture and performance matters, I would advise you to call directly a function from the ng-click.

ng-click="onClickSwitch()"

Then define the function onClickSwitch() in the $scope.

$scope.onClickSwitch = function() {
    $scope.switch = !$scope.switch;
    [code as above]
}

3 Comments

I'm not really sure how to implement it with my code, as it is slightly different, see updated plunkr (plnkr.co/edit/il5R7L8kOQwkCfPyetA9?p=preview)
plnkr.co/edit/i260kK85zTniDDk3nmVQ?p=preview Here is a fork without using $watch
I mark it as correct as the answer is more complete and related with my approach. Thanks!

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.