0

I'm having some problems on Angular 1.5.8 This is my Script:

 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.
                });

        };
};

but when i call it, i get this error on chrome console:

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)

This is my code in 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>

Obviously i have Person Class and added servlet path in web.xml. I think my problem is my bad code in Angular. What's the correct way to write controller and function in js file? THX

2 Answers 2

1

You forgot a couple of things. Beside the ); at the end of your Angular code, you have forgot to add dependencies as well. Try this:

//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.
                });

        };
}

EDIT:

For more details on $inject, you can check out this answer. Code above can be written like this:

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.
                });

        };
}]);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! It works. Can you explain me "app.controller('MyController', MyController);" and "MyController.$inject = ['$scope', '$http']"? Never seen before.
I will edit the question to the other option.
ok thank you very much!
1

you are missing ) at the end of code snippet, fix that and it should work fine.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.