0

The idea is to always have a new key as soon as you change the page, this is my code but I think I have problem

$scope.newKey =function (){
    var key =0;
    $rootScope.$on('$locationChangeSuccess', function () {
      key++ ;

    });
    return key;
};

If one of you has already tried $locationChangeSuccess it can help me thank you

Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
Youssef Mayoufi
  • 25
  • 1
  • 12

2 Answers2

0

Why don't you try using angular's service or factory, so that it includes the Scope variable key which is actually globally available to the entire app/module? i mean this is just my view though

Nishi Bangar
  • 720
  • 9
  • 20
0

I'm not pretty sure - how exactly you want to use your "newKey", but if you need it on your html, then the valverde93's answer is correct:
$scope.newKey = 0;
$rootScope.$on('$locationChangeSuccess', function () { $scope.newKey++; });
And on your html:
{{newKey}}
But if you want to use it on your backend (the js files like a function) - then you get always $scope.newKey = 0, just because you're put your console.log after the incrementation function, and you see only the $scope.newKey = 0;
So if you put your console.log in the function - something like:
$rootScope.$on('$locationChangeSuccess', function () { $scope.newKey++; console.log($scope.newKey);});
then you'll see your proper key.
Or you may to use $watch, to check when the scope is changed and to do somthing:
$scope.$watch('newKey', function(newValue ) { console.log( newValue ); } );
Good luck.

Fire Bull
  • 227
  • 3
  • 10