我有一些配置路由到不同的视图。它现在是硬编码的。有var“a”,ifit等于1,重定向到view1。但我想设置$ http并根据结果路由。
我如何在配置中插入$ http?
这是我的js:
angular.module('mainPage', [
'mainPage.controllers',
'ngRoute','kendo.directives'
]);
angular.module('mainPage').config(function ($routeProvider) {
//Here i want to put my $http.
var a = 1;
if(a==1){
$routeProvider.otherwise({ redirectTo: '/view1' });
}else{
bbb
}
$routeProvider.when('/view1', {
controller: 'Controller1',
templateUrl: 'partials/validation.html'
}).when('/view2', {
controller: 'Controller2',
templateUrl: 'partials/guests.html'
});
});
angular.module('mainPage', [])
.controller('Controller1',function($scope){
$scope.source = [
{nav_id:1,nav_name:"Validation",nav_src:"validation"},
{nav_id:2,nav_name:"Guests",nav_src:"guests"}
];
})
.controller('Controller2',function($scope){
$scope.now=new Date();
});
这是我的HTML:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>html { font-size: 12px; font-family: Arial, Helvetica, sans-serif; }</style>
<title></title>
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1411/styles/kendo.common.min.css" />
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1411/styles/kendo.default.min.css" />
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1411/styles/kendo.dataviz.min.css" />
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1411/styles/kendo.dataviz.default.min.css" />
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1411/styles/kendo.mobile.all.min.css" />
<script src="http://cdn.kendostatic.com/2014.3.1411/js/jquery.min.js"></script>
<script src="http://cdn.kendostatic.com/2014.3.1411/js/angular.min.js"></script>
<script src="http://cdn.kendostatic.com/2014.3.1411/js/kendo.all.min.js"></script>
</head>
<body>
<div ng-app="mainPage" ng-controller="MainCtrl">
<ng-view></ng-view>
</div>
<script src="js/angular-route.js"></script>
<script src="js/main_page.js"></script>
</body>
</html>
我想要这样的东西:
$http({/
url: 'aaa/aaa/aaa',
method: "GET",
headers: { 'Content-Type': 'application/json' }
}).success(function (data, status, headers, config) {
}
}).error(function (data, status, headers, config) {
});
答案 0 :(得分:0)
您无法注入配置,但请确保将这些内容放入&#39;运行&#39;块。
app.run(function($http) {
// use $http
})
how to inject dependency into module.config(configFn) in angular