2017-04-20 53 views
0

我在上角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

回答

1

你忘了一些事情。除了在Angular代码的末尾的);之外,您也忘记了添加依赖关系。试试这个:

//create module 
var app = angular.module('myApp', []); 

//add controler to module 
app.controller('MyController', MyController); 

//add dependecies 
MyController.$inject = ['$scope', '$http']; 

//controller function 
function MyController($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. 
       }); 

     }; 
} 

编辑:

,详细了解$inject,你可以看看这个answer。上面的代码可以这样写:

var app = angular.module('myApp', []); 
app.controller('MyController', ['$scope', '$http', 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. 
       }); 

     }; 
}]); 
+0

谢谢!有用。你能解释我“app.controller('MyController',MyController);”和“MyController。$ inject = ['$ scope','$ http']”?从未见过。 – boso92

+0

我将编辑问题到其他选项。 – eminlala

+0

好的非常感谢你! – boso92

1

你缺少)在代码片段的末尾,修复它,它应该工作正常。