3

I'm new to complex angular directives. I have a variable in the rootScope called $root.page. I have set up a watch like so:

$scope.$watch(
  "$root.page",
  function handleChange( newValue, oldValue ) {
    //show the select page only on page change

    console.log(oldValue, newValue);

    if (!oldValue || oldValue !== newValue) {
      $scope.showSelectOnly = true;
    }
  }
);

The way my code is currently structured, $scope.showSelectOnly is always true. I only want $scope.showSelectOnly to be true when there is a change to $root.page and I want it to be false in all other cases (i.e. when there is no change to the $root.page variable).

5
  • What about adding else statement and assigning false value to $scope.showSelectOnly ? Commented Feb 5, 2016 at 18:29
  • I literally just tried that 2 minutes ago and it worked. I didnt' realize $watch worked like that and you could check for 'non-changes' also. I guess this is the case for $watch over ng-change? Commented Feb 5, 2016 at 18:35
  • I spoke to soon - that didn't work Commented Feb 5, 2016 at 18:42
  • I'm get the logic but I don't get the use-case. What are you trying to achieve from the User eXperience? There may be a better approach to this. Commented Feb 5, 2016 at 18:56
  • Thought I answered earlier, sorry. there are three columns. in the first there are two sections that group cases of work items. when you click on either, a list of items shows up respective to each case, in the second column. In the third column, I want a div with "Select an item" to show up. whenever you click anywhere in the first column, I want the 'select an item' to show up in the third column. otherwise, no need for that div. I ended up solving this with $rootScope so I'm still looking for good answers Commented Feb 5, 2016 at 20:33

2 Answers 2

1

I don't think you need a watch for this case. You might want to look into using angular value instead. You have access to angular value in your entire angular module app. With that, you can just update showSelectOnly when page is changed.

angular.module('myApp', [])
  .value('page', true)
  .value('showSelectOnly', true);

You can then inject page into any controller in your module, and update both values as needed.

angular.module('myApp')
  .controller('myCtrl', function (page, showSelectOnly) {
      if (!page) {
         showSelectOnly = true;
      }
  });
Sign up to request clarification or add additional context in comments.

1 Comment

I believe the value should be a hash with the key "page" & "showSelectOnly" otherwise they will have to go through a rather indirect processes for changing the construct stored in the value.
0

Here is the code you need:

demo

HTML:

<div ng-app="app" ng-controller='myCtrl as vm'>

  <button ng-click="$root.page = 1">Change root Page</button>
  <label>
     showSelectOnly: {{showSelectOnly}}
  </label>

</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.js"></script>

javascript:

angular.module('app', [])
 .controller('myCtrl', function($scope) {
  var vm = this;
  $scope.showSelectOnly = false;

  $scope.$watch(
    "$root.page",
    function handleChange(newValue, oldValue) {
      if (newValue !== oldValue) {
        $scope.showSelectOnly = true;
      }
    }
  );
})

3 Comments

Your demo replicates my problem :) I clicked the button once and it stayed at true forever, it didn't go back to false once there was no change
the first state is false then you click the button the state is change and it set to true. do you want it to change to false in case another click done?
Yep, as I mentioned in my question, I want it to be false in all other cases (i.e. when there is no change to the $root.page variable).

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.