我在这里有一个非常简单的 Angular 页面
<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
最佳答案
只是指出这一点是因为我不能因rep低而无法评论,但是您也正在调用ng-app="myApp"
,它正在寻找myApp
的模块调用,而您没有myApp
的模块调用,因此您的 Controller 可能无法正确触发您的myApp
到app
?