I am using Angular's UI Router and have a resolver setup for a particular view and controller which looks something like this as you can see below. How would I go about updating the URL with a value that is returned from my urlHasherFactory
factory? I would like the value that this factory generates to be populated in my URL bar such as: http://example.com/user/57
. In this case 57
is the dynamic user value I would like to populate and will vary depending on what's returned by the factory.
var MYAPP = angular.module('myApp', ['ui.router'])
.config(['$stateProvider', '$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
// For any unmatched url redirect to /
$urlRouterProvider.otherwise("/");
// States
$stateProvider
// Start
.state('start', {
url: "/",
templateUrl: "views/_start.html",
controller: "StartCtrl"
})
// Chat
.state('chat', {
url: "/user/:userId",
templateUrl: "views/_user.html",
controller: "UserCtrl",
resolve: {
user: function(urlHasherFactory) {
return urlHasherFactory.getUrlHash();
}
}
});
}
])
.factory('urlHasherFactory', ['$http', function ($http) {
var hash = {
getUrlHash: function() {
var promise = $http({ method: 'GET', url: '/userid' })
.success(function(data, status, headers, config) {
return data;
}
);
return promise;
}
};
return hash;
}])
.controller('StartCtrl', ['$scope', function($scope) {
// How do I update URL structure in here?
}])
.controller('UserCtrl', ['$scope', 'user', function($scope, user) {
// How do I update URL structure in here with user's id
// so it looks something like this: http://example.com/user/57
// This is I how I access urlHasherFactory
var urlHash = user.data.userid;
}]);