2016-01-07 67 views
0

我想寫一個非常小的應用程序,它所做的只是從外部服務獲取數據,外部服務接受一個路徑並返回該路徑中的文件和目錄,把它看作是一個文件瀏覽器。當使用ngRoute時,一切正常,但我想轉換爲ui.router,因爲我必須更新頁面的多個部分。這個問題來自事實,我的路徑似乎並沒有工作,因爲我總是重定向到base狀態,我認爲是因爲$urlRouterProvider.otherwise('/');AngulaJS ui路由器:路由到url狀態

我對JavaScript和AngularJS非常新,我花了很多時間幾天試圖找到沒有多少運氣解決這個問題,所有的文件我看到的總是用一個靜態的URL像/demo/{id}/{info}在我的網址是動態/repos/{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; 
    } 
    }); 

and t爲表自己的局部視圖列表中的文件/目錄

<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>沒有運氣,特別是因爲回購路徑需要過濾

+0

在你的'ui-sref'的例子中,你有'contacts.detail(...)'是它在你的代碼中的實際情況或...? – jsonmurphy

+0

我的代碼,或者我沒有碰到的東西是'{{ directory.name }}' – Telmo

+0

你不能在'repos.path'上調用一個函數,並且通過在你的控制器中注入'$ filter'服務來爲你做過濾器嗎?即'{{ directory.name }}' – jsonmurphy

回答

0

該問題可能與您的狀態定義。按照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' 
     }) 
+0

根據文檔,它們都是有效的'/files/{path:.*}' - 匹配任何以'/ files /'開頭的URL,並將路徑的其餘部分捕獲到參數'path'中。 '/ files/* path' - 同上。抓住所有的特殊語法。 – Telmo

+0

真的......甚至沒有注意到這個例子。 – jsonmurphy

0

第一觀察:兩個默認的(「」)狀態的看法指定相同templateUrl。這意味着您將爲您的兩個網址顯示相同的模板。我意識到,但是你指定'重定向'。第二種觀察:你的'其他'功能在狀態路由之前運行。這可能意味着在你的狀態路由之前,你的狀態會被匹配,這會導致同樣的問題。我可能是錯的。

+0

templateUrl部分是有意完成的,'/'url顯示了「root」文件系統,而'/ repos/* path'顯示了「路徑」內的文件,您可能還會注意到''RepoController'和'RepoListCtrl'是相同的。 – Telmo