不能在angular.js中使用$ httpProvider和locationProvider

时间:2014-09-03 20:36:33

标签: angularjs

我是angular.js的新手。我使用$ httpProvider和$ locationProvider

  <script src="lib/angular/angular.min.js"></script>
  <script src="lib/angular/angular-route.min.js"></script>
  <script src="lib/angular/angular-cookies.min.js"></script>
angular.module('myApp', [
  'ngRoute','ngCookies'  
]).config(['$routeProvider' , '$locationProvider', '$httpProvider' ,function($routeProvider) {
  $httpProvider.defaults.timeout = 5000;  // error
  $locationProvider.html5Mode(true);  // error
}])

当应用程序运行时,它出现错误:未捕获错误:[$ injector:modulerr] http://errors.angularjs.org/1.3.0-beta.2/ $ injector / modulerr?p0 = myApp&amp; p1 = Ref / localhost / lib / angular / angular。 min.js)

我的angular-min.js,angular-route.js,angular-cookie.js都在1.3.0 beta.2中,不知道为什么会发生这种情况?

4 个答案:

答案 0 :(得分:3)

我认为您忘记添加$locationProvider$httpProvider作为配置函数的参数。

试试这个:

  <script src="lib/angular/angular.min.js"></script>
  <script src="lib/angular/angular-route.min.js"></script>
  <script src="lib/angular/angular-cookies.min.js"></script>
   angular.module('myApp', [
  'ngRoute','ngCookies'  
   ]).
   config(['$routeProvider' , '$locationProvider', '$httpProvider', 
   function($routeProvider, $locationProvider, $httpProvider) {
   $httpProvider.defaults.timeout = 5000;  
   $locationProvider.html5Mode(true);  
   }])

答案 1 :(得分:1)

您忘记注入$locationProvider$httpProvider作为参数。

var app = angular.module('myApp', ['ngRoute','ngCookies']);

app.config(['$routeProvider' , '$locationProvider', '$httpProvider', function ($routeProvider, $locationProvider, $httpProvider) {
      $httpProvider.defaults.timeout = 5000;  // error
      $locationProvider.html5Mode(true);  // error
}]);

答案 2 :(得分:1)

首先,您应该添加以下脚本:

<script src="lib/angular/angular.min.js"></script>
<script src="lib/angular/angular-route.min.js"></script>
<script src="lib/angular/angular-cookies.min.js"></script>

然后你应该配置:

var app = angular.module('myApp', ['ngRoute','ngCookies']);

app.config(['$routeProvider' , '$locationProvider', '$httpProvider', function ($routeProvider, $locationProvider, $httpProvider) {
      $httpProvider.defaults.timeout = 5000;
      $locationProvider.html5Mode(true); 
}]);

注意:为避免错误:[$ location:nobase] HTML5模式下的$ location需要标记!)不要忘记添加:

<head>
...
...
<base href="/">
...
</head>

答案 3 :(得分:0)

在回调函数之前添加的字符串(在配置,控制器或指令中)是在JS缩小的情况下允许angular匹配缩小的变量及其含义。

因此,您应该将所需的变量添加为参数:

angular.module('myApp', [
  'ngRoute','ngCookies'  
]).config(
['$routeProvider' , '$locationProvider', '$httpProvider',
function($routeProvider,  $locationProvider, $httpProvider) {
    $httpProvider.defaults.timeout = 5000;  
    $locationProvider.html5Mode(true);  
}])