当用户导航到$scope.lessons时,我希望/writeups/example.json得到/tutorials/example

我的app.js文件已启用html5模式,如下所示:

.when('/', {templateUrl: 'views/home.html', controller: 'HomeCtrl'})
.when('/tutorials/:topic', {templateUrl: 'views/default.html', controller: 'AppCtrl'})
.otherwise({ redirectTo: '/'});


我目前在控制器中

$scope.lessons = LessonService.get({topic: $routeParams.topic});

我的工厂看起来像:

app.factory('LessonService', ['$resource', function($resource){
    return $resource('writeups/:topic.json', {}, {
        query: {method:'GET', params:{topic:'bc'}, isArray:true}
    });
}]);


更新:这是我的完整控制器

app.controller('NavCtrl', ['$scope', 'LessonService', '$routeParams', '$mdSidenav', '$timeout','$log', function($scope, $routeParams, LessonService, $mdSidenav, $timeout, $log) {
    function toggleSideNav(name){
        $mdSidenav(name).toggle();
    }

    function selectLesson(name){
        $scope.page = name.Path;
        $scope.toggleSideNav('left');
    }

    $scope.lessons = LessonService.LessonService.get({topic: $routeParams.topic});
    $scope.orderProp = 'Id';
    $scope.toggleSideNav = toggleSideNav;
    $scope.selectLesson = selectLesson;
    $scope.page = 'views/defaultLesson.html';
}]);

最佳答案

您只需要在控制器中包含$ routeParams即可,如下所示:

angular.module('yourApp')
     .controller('yourCtrl',["$scope", "$routeParams", function ($scope, $routeParams) {
      console.log($routeParams)

}]);

09-19 16:59