我在上角1.5.8 一些问题,这是我的脚本:Angularjs 1.5.8脚本错误:如何编写正确的代码?
var app = angular.module('myApp', []);
app.controller('MyController', function($scope,$http) {
$scope.getDataFromServer = function() {
$http({
method : 'GET',
url : 'javaAngularJS'
}).success(function(data, status, headers, config) {
$scope.person = data;
// this callback will be called asynchronously
// when the response is available
}).error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
};
};
,但是当我把它,我得到的铬控制台上的错误:
angular.js:13920 Error: [ng:areq] http://errors.angularjs.org/1.5.8/ng/areq?p0=MyController&p1=not%20a%20function%2C%20got%20undefined
at angular.js:38
at sb (angular.js:1892)
at Pa (angular.js:1902)
at angular.js:10330
at ag (angular.js:9451)
at p (angular.js:9232)
at g (angular.js:8620)
at angular.js:8500
at angular.js:1763
at m.$eval (angular.js:17682)
这是我的代码在JSP:
<div ng-app="myApp">
<div ng-controller="MyController">
<button ng-click="getDataFromServer()">Fetch data from server</button>
<p>First Name : {{person.firstName}} </p>
<p>Last Name : {{person.lastName}} </p>
</div>
</div>
显然我有Person类和在web.xml加入servlet路径。我认为我的问题是我在Angular中糟糕的代码。在js文件中写入控制器和函数的正确方法是什么? THX
谢谢!有用。你能解释我“app.controller('MyController',MyController);”和“MyController。$ inject = ['$ scope','$ http']”?从未见过。 – boso92
我将编辑问题到其他选项。 – eminlala
好的非常感谢你! – boso92