I realise that this problem has been presented before. But none of the answers appear to solve this problem.
I have a moderate knowledge of Angular and have produced quite a lot of angular code, I'm not a complete beginner.
I have started working on a MEAN app, so all the client side is Angular. And after two days of banging my head on the wall, I can't get a perfectly simple location.path(newPath) to work in a function in the $scope of a controller.
'use strict';
angular.module('core').controller('HeaderController',
[ '$scope',
'$location',
function($scope, $location) {
//THIS LINE OF CODE, ON ARRIVAL IN THE CONTROLLER, WORKS
//- it changes the path no problem.
//$location.path('/it/vino/chianti/ChiantiClassico');
//But trying to set $location.path() DOES NOT WORK IN THE
//FOLLOWING FUNCTION.
//This function is called correctly from an ng-click event on a link.
//Therefore there should be no need to call apply().
//However I have also tried both $apply and $timeout.
$scope.changeLanguage = function(lang){
//Right, let's try setting the path..
$location.path('/it/vino/chianti/ChiantiClassico');
//now we log $location.path() in the console and it
//gives the correct answer, which is:
//'/it/vino/chianti/ChiantiClassico',
//(but the path does not actually change in the browser address bar).
console.log('$location.path():', $location.path()); //correct!
//But if I also log the location object and I look inside,
//the path hasn't changed there !
console.log('$location:', $location); //The path is the old one!
//And the address bar has not changed
};
}//end function
]);
I have debugged and scoured the objects looking for another $location object or some other mistake. - But there is only one $location object - found in the 'closure' section of the stack.
I can't see any kind of callback mistake here, I'm not trying to use an object that hasn't yet been defined or something like that.
Here is the calling link code from the view for completeness, nothing strange here, I've tried calling from buttons as well, the function is called correctly:
<a href="javascript:;" data-ng-click="changeLanguage('it')">italiano</a>