javascript - 如何在AngularJs中通过$stateProvider传递参数?

标签 javascript angularjs angular-ui-router

我想将 id 参数从 URL 传递到 Controller 。

这是我的配置文件

$stateProvider.state('main.edit_tutor', {
        url : '/home/edit/:id', // here I want to pass Id
        views:{
            "header":{ templateUrl : 'templates/header.html',
                    controller : 'HeaderController',
            },
            "sidebar":{ templateUrl : 'templates/sidebar.html',
                        controller:'SidebarController',
            },
            "main":{ templateUrl : 'templates/editTutor.html',
                    controller:'TutorEditController',
            },
            "footer":{ templateUrl : 'templates/footer.html',
            },

        }
      })

我想要 TutorEditController 中的 id 参数

    app.controller('TutorEditController', function($scope, $rootScope, $stateParams, $state, LoginService,FetchService,UtilityService,$filter,UpdateService,$routeParams) { 

    $scope.id = $routeParams.id;

});

我尝试像这样传递 id

http://localhost/tutor_crm/#/main/home/edit?id=4
http://localhost/tutor_crm/#/main/home/edit/id=4
http://localhost/tutor_crm/#/main/home/edit/4

我收到类似 Error: [$injector:unpr]

的错误

最佳答案

该错误是因为您正在注入(inject)用于 ngRouterouteParams,但您使用了 angular-ui-router

由于您使用的是angular-ui-router,因此您应该使用$stateParams而不是$routeParams

app.controller('TutorEditController', function($scope, $rootScope, $stateParams, $state, LoginService,FetchService,UtilityService,$filter,UpdateService) { 
    $scope.id = $stateParams.id;
});

URL 应该是,

http://localhost/tutor_crm/#/main/home/edit/4

关于javascript - 如何在AngularJs中通过$stateProvider传递参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42529117/

相关文章:

javascript - Jquery 不适用于动态添加的按钮?

javascript - 无法读取未定义的属性

javascript - 在 ng-repeat 中使 ng-disabled 为真

angularjs - 在初始化 Angular 应用程序之前等待 $http 结果

javascript - AngularJS 查询参数

javascript - 图像左侧的空白

javascript - ng-click 的 angularjs Restful 应用程序问题

javascript - 我应该将 deviceready 放在多页 PhoneGap 应用程序的什么位置?

javascript - 在 angularJs 中从 Controller 调用同时 $http 服务?

javascript - AngularJS 中的路由