0

I am new to AngularJS. I am trying learn to pass the value of a variable to the angularJS. But for some unknown reason, I am not being able to do.

Below is the .jsp page which I think is correct:

<body >
    <div ng-controller="customersController">
        <label>Value</label>
        <input type="text" ng-model=something></input>
        <button ng-click="punchIt()">click me!</button>
        <br>Obtained value : {{ value }}
    </div>
</body>

In the respective .js page, value passed from .jsp file is not getting retained.The first alert function should return assumed : something's value,but it is returning assumed : undefined. Below is the .js file:

var myApp = angular.module("getInfo", []);

myApp.controller("customersController", function($scope, $http){

    $scope.punchIt = function ($scope, $http) {

        var data = {Value: $scope.something};
        alert("assumed : "+ $scope.something);
        $http.post("http://localhost:8082/HelloWorldWS/services/HelloWorldImpl",data)
            .success(function (data, status, header) {
                alert("in success " + data);
                $scope.value = data; 
          }).error(function (data) {
                alert("in error method " + status);
                $scope.value = "error "+ data;
         });
        };
});

Please suggest some way out. Thanks.

1
  • ng-model=something will obviously not insert a JSP variable.. Isn't there a syntax for writing JSP to the view? Commented Oct 9, 2014 at 15:56

1 Answer 1

1

You are expecting two variables ($scope and $http) in your punchIt() function declaration but not passing these when you call it on button click. Thus inside your punchit() function, both $scope and $http variables would be initialized to nothing (read undefined).

You actually dont need to pass these parameters to your function. Your controller already has these services injected in it via your controller declaration.

Also declare/initialize the name variable in your controller. Else, if you do not enter anything in the input field and try to access it in your $scope, you will retrive it as undefined.

Your code changes would look as below:

myApp.controller("customersController", function($scope, $http){
    $scope.name=null;
    //OR $scope.name='';
    $scope.punchIt = function () {
    ...
    }


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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.