I have this code...
spa.factory("linkFactory", function() {
var linkFactoryObject = {};
linkFactoryObject.currentLink = "home";
return linkFactoryObject;
});
This snippet acts as a global variable which I set and read between different controllers. This variable controls which link is marked active.
My code works really well. The only problem is that if I don't set an initial value in the factory when you first enter my web page the home
link will not be set to active. If I do set an initial value then when someone refreshes a page the value will reset to the initial value. This means that the home page link will be active, but you will actually be on a different page.
Additionally, I've tried to use $location.path()
to get my path and build a function off of that value, but $location.path()
is static, so this won't work either.
I have this route snippet which controls which content I load...
spa.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/home', {
templateUrl: '/partials/home.html'
})
.when('/about', {
templateUrl: '/partials/about.html'
})
.when('/skills', {
templateUrl: '/partials/skills.html'
})
.when('/experience', {
templateUrl: '/partials/experience.html'
})
.when('/resume', {
templateUrl: '/partials/resume.html'
})
.when('/contact', {
templateUrl: '/partials/contact.html'
})
.otherwise({
redirectTo: '/home'
});
});
If I could somehow set a variable here that I could read in my controllers I would be good to go, but I'm unsure how to do that.
How would I accomplish this?