0
我正在使用Angular的UI路由器,併爲特定的視圖和控制器設置瞭解析器,它看起來像這樣,如下所示。我將如何使用從我的urlHasherFactory
工廠返回的值更新URL?我希望該工廠生成的值填充到我的URL欄中,例如:http://example.com/user/57
。在這種情況下,57
是我想要填充的動態用戶值,並且取決於工廠返回的內容。從工廠解析器返回值更新URL?
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;
}]);
看一下http://stackoverflow.com/questions/11534710/angularjs-how-to-use-routeparams-in-generating-the-templateurl – MichaelLo