我是angularjs的新手,下面的部分总是让我很困惑
angular.module('bookYourSeatApp', [])
.factory('seats', SeatsFactory)
.controller('mainCtrl', MainCtrl);
我想获得routeparams并尝试
function SeatsFactory($rootScope, $timeout, $routeParams) {
和
function MainCtrl(seats, $routeParams) {
并在其他几个地方插入$ routeparams,我到处都是错误。代码位于fiddle。如何在下面的代码中注入$ routeparams?
angular.module('bookYourSeatApp', [])
.factory('seats', SeatsFactory)
.controller('mainCtrl', MainCtrl);
function SeatsFactory($rootScope, $timeout) {
//more code
var factory = {
map: seats,
setSeats: setSeats,
select: selectSeats,
checkedSeats: checkedSeats,
availCount: {},
setAvailCount: function(count) {
console.log('avail', count);
checkSelected(count);
}
};
return factory
}
function MainCtrl(seats) {
// console.log($routeParams);
var vm = this;
angular.extend(vm, {
seats: seats,
selectionCount: [//[0,1,2,3,4],[
{id: 0, val: 0}, // object for two-way binding
],
selectedCount: 0
});
vm.selectedCount = vm.selectionCount[0];
seats.setAvailCount(vm.selectedCount);
}
答案 0 :(得分:0)
angular.module('bookYourSeatApp', [])
.factory('seats', SeatsFactory)
.controller('mainCtrl', MainCtrl);
MainCtrl.$inject = ['$routeParams'];
function SeatsFactory($rootScope, $timeout) {
//more code
var factory = {
map: seats,
setSeats: setSeats,
select: selectSeats,
checkedSeats: checkedSeats,
availCount: {},
setAvailCount: function(count) {
console.log('avail', count);
checkSelected(count);
}
};
return factory
}
function MainCtrl(seats, $routeParams) {
console.log($routeParams);
var vm = this;
angular.extend(vm, {
seats: seats,
selectionCount: [//[0,1,2,3,4],[
{id: 0, val: 0}, // object for two-way binding
],
selectedCount: 0
});
vm.selectedCount = vm.selectionCount[0];
seats.setAvailCount(vm.selectedCount);
}
我建议不要将$ routeParams作为服务的init参数注入。相反,将它注入controllers.factory是singleton
答案 1 :(得分:0)
在创建工厂和控制器之前,您可能错过了这些注射
SeatsFactory.$inject = ['$rootScope', '$timeout'] //make sure if you need 2 params in your SeatsFactory function, inject 2 params as well here
MainCtrl.$inject = ['seats', '$routeParams'] //as well the controller
请记住:服务/工厂是一个单独的共享资源,您可能不希望这个单例接收到一些依赖的参数,只需传递服务/工厂需要继续的数据。
希望它有所帮助。