我想将 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)用于 ngRoute
的 routeParams
,但您使用了 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/