-在角度,我们可以注入$routeProvider到config功能
module.config(function ($routeProvider) {
});
Run Code Online (Sandbox Code Playgroud)
我想把我的服务注入其中
module.config(function ($routeProvider, myService) {
});
Run Code Online (Sandbox Code Playgroud)
我确信该服务已正确定义,但它会抛出一个异常unknown myService,说明当我注入时的事件
module.config(function ($routeProvider, $http) {
});
Run Code Online (Sandbox Code Playgroud)
它仍然说unknown $http.
你知道为什么吗?
我正在使用angularjs网站,并且在Rails中使用路由以及在php中使用Laravel.通过Laravel中的路线,我们可以动态创建一组类似于以下的路线:
foreach($cities as $city):
Route::get($city.'/hotels');
Route::get($city.'/{slug}');
endforeach;
Run Code Online (Sandbox Code Playgroud)
在这里,我们定义一系列Laravel单独的路线在技术上做看起来是一样的,除了价值的city和slug.
在这种情况下,我发现angularJS在定义路径时有点受限.坦率地说,我有点迷失在这里.
我在这里做了一些修改 - 基本上我设置了一个服务,它从我的数据库中检索资产,例如在这种情况下的城市和类别列表.我正在尝试这样做:
如果{slug}在我的API中检索的类别数组中,则使用my ListController和list视图,但如果不是,则使用我的SingleVenueController单视图.这是我目前的代码,但它无法正常工作:(
appRouteProvider.when('/:city/:slug', {
templateUrl : function(sharedParams, $routeParams){
t = sharedParams.getCurrentPageType($routeParams);
if(t=='list'){
return '../../app/templates/list.html';
}
if(t=='single'){
return '../../app/templates/single.html';
}
},
controller : function(sharedParams, $routeParams){
t = sharedParams.getCurrentPageType($routeParams);
if(t=='list'){
return 'ListsController';
}
if(t=='single'){
return 'SingleController';
}
},
resolve:{
sharedParamsData:function(sharedParams){
return sharedParams.promise;
},
}
})
Run Code Online (Sandbox Code Playgroud)
在上面sharedParams是一个服务,getCurrentPageType只是检查url slug来决定要发回的控制器 - 但它根本不工作:(
假设我需要在用户发出的每个请求中包含GroupId参数,但我不想修改每个服务调用以包含它.是否可以将GroupId自动附加到所有请求,无论是POST还是GET查询字符串?
我一直在研究拦截器request功能,但无法弄清楚如何进行更改
**编辑**
下面的当前工作样本是Morgan Delaney和haimlit建议的组合(我认为无论如何它都是一个组合).基本思想是,如果请求是POST,请修改config.data.对于GET,请修改params.到目前为止似乎工作.
还不清楚提供者系统如何在Angular中工作,所以我不确定在这里修改data.params属性是否完全合适.
.config(['$httpProvider', function ($httpProvider) {
$httpProvider.interceptors.push(['$rootScope', '$q', 'httpBuffer', function ($rootScope, $q, httpBuffer) {
return {
request: function (config) {
if (config.data === undefined) {
//Do nothing if data is not originally supplied from the calling method
}
else {
config.data.GroupId = 7;
}
if (config.method === 'GET') {
if (config.params === undefined) {
config.params = {};
}
config.params.GroupId = 7;
console.log(config.params);
}
return config;
}
};
} ]);
} …Run Code Online (Sandbox Code Playgroud) 我需要$http在config方法中使用service.但我无法$http在配置中使用,我收到unknown Provider错误消息.
我的代码:
.config(function($http, $routeProvider, $provide) {
$http.get("sampleslist.js").success(function(data) {
var loop = 0, currentRoute;
for (loop = 0; loop < data[loop].pages.length; loop++) {
currentRoute = data[loop].pages;
var routeName = "/" + currentRoute[loop].name;
$routeProvider.when(routeName, {
templateUrl:"/" + currentRoute.name + ".html",
})
}
})
app = {controller: $controllerProvider.register}
})
Run Code Online (Sandbox Code Playgroud)
你能为此提供解决方案吗?