未捕获的TypeError:app.all不是函数

时间:2017-02-03 20:36:55

标签: javascript jquery angularjs

我这里有一个非常简单的角度页面

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myCtrl">

  <p>Today's welcome message is:</p>
  <h1>{{myData}}</h1>

</div>

<script>

  var app = angular.module('corsApp', []);

  app.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];

  }

  ]);


  // Added all header request and response.
  app.all('/*', function (request, response, next) {
    response.header("Access-Control-Allow-Origin", "*");
    response.header("Access-Control-Allow-Headers", "X-Requested-With");
    response.header("Access-Control-Allow-Methods", "GET, POST", "PUT", "DELETE");
    next();
  });



  app.controller('myCtrl', function($scope, $http) {
    $http.get("welcome.htm")
    .then(function(response) {
        $scope.myWelcome = response.data;
    });
});


</script>

我一直在控制台里拿到这个。

未捕获的TypeError:app.all不是函数

我该如何预防?

我跟着这个:

https://stackoverflow.com/a/21455819/4480164

1 个答案:

答案 0 :(得分:2)

指出这一点是因为由于低代表我无法发表评论,但您也在调用ng-app="myApp"正在寻找myApp的模块调用,并且您没有myApp的模块调用1}}所以您的控制器无法正确触发,或者重新将myApp改为app