[英]AngulaJS ui-router: routing to url state
我正在尝试编写一个非常小的应用程序,它所做的全部工作均从外部服务获取数据,外部服务接受路径并返回该路径中的文件和目录,将其视为文件浏览器。 使用ngRoute
时,一切工作正常,但是由于我必须更新页面的多个部分,因此我尝试转换为ui.router
。 问题来自这样一个事实:我认为由于$urlRouterProvider.otherwise('/');
我总是被重定向到base
状态,所以我的路径似乎不起作用$urlRouterProvider.otherwise('/');
我是javascript和AngularJS的新手,我花了很多天的时间来寻找解决此问题的方法,但运气不佳,我看到的所有文档始终使用静态网址,例如/demo/{id}/{info}
,其中我的网址是动态/repos/{path}
,其中path是实际路径,例如/tmp/test/test1/test2
这是当前代码
var app = angular.module("atlas", ['ui.router', 'repoControllers']);
app.config(['$stateProvider', '$urlRouterProvider',
function ($stateProvider, $urlRouterProvider){
$urlRouterProvider.otherwise('/');
$stateProvider
.state('base', {
url: '/',
views: {
'': { templateUrl: 'partials/repo-list.html' },
'repoinfo': { templateUrl: 'partials/repo-info.html' },
'repoownership': { templateUrl: 'partials/group-ownership.html' },
'repotype': { templateUrl: 'partials/repo-type.html' },
'repopath': { templateUrl: 'partials/repo-path.html' },
'userinfo': { templateUrl: 'partials/user-info.html' }
},
controller: 'RepoController'
})
.state('base.repos', {
url: '/repos/*path',
parent: 'base',
views: {
'': { templateUrl: 'partials/repo-list.html' },
'repoinfo': { templateUrl: 'partials/repo-info.html' },
'repoownership': { templateUrl: 'partials/group-ownership.html' },
'repotype': { templateUrl: 'partials/repo-type.html' },
'repopath': { templateUrl: 'partials/repo-path.html' },
'userinfo': { templateUrl: 'partials/user-info.html' }
},
controller: 'RepoListCtrl'
})
}]);
app.factory('atlasRepository', function($http) {
return {
getIndex: function(path) {
console.log("FACTORY: getIndex " + path);
var sanitized = "";
if (typeof path === 'undefined') {
sanitized = '/';
} else {
sanitized = "/" + path.replace(/\/+/g, '/');
};
var url = "http://localhost:8080" + sanitized;
console.log(url);
return $http.get(url);
}
};
});
app.controller("RepoController", function($scope, $stateParams, atlasRepository) {
$scope.repos = atlasRepository.getIndex($stateParams.path).success(function(repos) {
$scope.repos = repos;
console.log("RepoController");
});
});
var repoControllers = angular.module('repoControllers', []);
repoControllers.controller("RepoListCtrl", function($scope, $stateParams, atlasRepository) {
$scope.repos = atlasRepository.getIndex($stateParams.path).success(function(repos) {
$scope.repos = repos;
console.log($stateParams);
console.log($scope);
});
});
app.filter('bytes', function() {
return function(bytes, precision) {
if (isNaN(parseFloat(bytes)) || !isFinite(bytes)) return '-';
if (typeof precision === 'undefined') precision = 1;
var units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'],
number = Math.floor(Math.log(bytes) / Math.log(1024));
return (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision) + ' ' + units[number];
}
});
app.filter('path', function() {
return function(path) {
path = path.replace(/^\//, '');
return path;
}
});
以及列出文件/目录的表的部分视图
<table>
<tr>
<th>File Name</th>
<th>Size</th>
<th>Modified Date</th>
<th>User</th>
<th>Action</th>
</tr>
<tr ng-repeat="directory in repos.directories">
<td><a href="#/repos/{{repos.path| path}}{{directory.name}}/"><i class="fa fa-folder"></i> {{directory.name}}</a></td>
<td>-</td>
<td> {{directory.updatedat | date:"medium"}}</td>
<td>unknown</td>
<td><i class="fa fa-trash"></i></td>
</tr>
<tr ng-repeat="file in repos.files">
<td><i class="fa fa-file"></i> {{file.name}}</td>
<td> {{file.size | bytes}}</td>
<td> {{file.updatedat| date:"medium"}}</td>
<td>unknown</td>
<td><i class="fa fa-trash"></i></td>
</tr>
</table>
我尝试在模板中使用ui-sref
,但所有文档再次指向相当简单的用例<a ui-sref="contacts.detail({ id: contact.id })">{{ contact.name }}</a>
在我的情况下不起作用,因为我需要传递当前路径以及要更改为的目录名,我尝试了<a ui-sref="contacts.detail({ id: repos.path + directory.name })">{{ directory.name }}</a>
没有运气,特别是因为需要过滤repos.path
问题可能出在您的状态定义上。 根据docs ,通配符应指定为:
.state('base.repos', {
url: '/repos/{path:.*}',
parent: 'base',
views: {
'': { templateUrl: 'partials/repo-list.html' },
'repoinfo': { templateUrl: 'partials/repo-info.html' },
'repoownership': { templateUrl: 'partials/group-ownership.html' },
'repotype': { templateUrl: 'partials/repo-type.html' },
'repopath': { templateUrl: 'partials/repo-path.html' },
'userinfo': { templateUrl: 'partials/user-info.html' }
},
controller: 'RepoListCtrl'
})
第一次观察:两个默认('')状态视图都指定相同的templateUrl。 这意味着您将为两个URL显示相同的模板。 我知道,但是您指定了“重定向”。
第二个观察:您的“否则”功能在状态路由之前运行。 这可能意味着在状态路由之前,您的其他对象将被匹配,这将导致相同的问题。 我对此可能是错的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.