Javascript 如何通过$stateProvider传递AngulrJs中的参数?
我想将一个id参数从URL传递给控制器 这是我的配置文件Javascript 如何通过$stateProvider传递AngulrJs中的参数?,javascript,angularjs,angular-ui-router,Javascript,Angularjs,Angular Ui Router,我想将一个id参数从URL传递给控制器 这是我的配置文件 $stateProvider.state('main.edit_tutor', { url : '/home/edit/:id', // here I want to pass Id views:{ "header":{ templateUrl : 'templates/header.html', controller : 'HeaderCon
$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
app.controller('TutorEditController', function($scope, $rootScope, $stateParams, $state, LoginService,FetchService,UtilityService,$filter,UpdateService,$routeParams) {
$scope.id = $routeParams.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
我得到的错误类似于
错误:[$injector:unpr]
错误是因为您正在注入routeParams
,这是用于ngRoute
,但您使用的是angular ui router
由于您使用的是角度ui路由器
,因此应使用$stateparms
而不是$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
使用$stateParams
而不是$routeParams
很高兴为您提供帮助。如果你觉得我的回答有用的话,你可以接受
app.controller('TutorEditController', function($scope, $rootScope, $stateParams, $state, LoginService,FetchService,UtilityService,$filter,UpdateService) {
$scope.id = $stateParams.id;
});