2
  <form class="form-group has-success has-feedback" novalidate>
                <br/>
                <label class="control-label" for="inputSuccess2">Enter the Phone Number</label>
                <input type="number" class="form-control" ng-model="user.phonenum" name="phonenum" id="inputSuccess2">

                <button ng-click="myData.doClick(item, $event)" class="btn btn-primary">Get Mobile Phone Number Data</button>
            </form>

            <br/>
            Data from server: {{myData.fromServer.State}}


      <script>
        angular.module("myapp", [])
                .controller("MyController", function($scope, $http) {
                    $scope.myData = {};
                    $scope.myData.doClick = function(item, event) {

                        var responsePromise = $http.get("https://ajith-indian-mob-info.p.mashape.com/getInfo?mobno="+item, {headers: {'X-Mashape-Key': ''}});
                        responsePromise.success(function(data, status, headers, config) {
                            $scope.myData.fromServer = data;
                        });
                        responsePromise.error(function(data, status, headers, config) {
                            alert("AJAX failed!");
                        });
                    }


                });
    </script>

Hi i am new to angular how do i get the value from textbox into the controller angular?

3 Answers 3

3

Personally in my controller, I define the object user (for a better reading, this is not required)

$scope.user = {}

Then

<input type="number" class="form-control" ng-model="user.phonenum" name="phonenum" id="inputSuccess2">

will automatically bind the content of this textbox to the variable $scope.user.phonenum, you can access it, simply with :

$scope.myData.doClick = function() {
    var responsePromise = $http.get("https://ajith-indian-mob-info.p.mashape.com/getInfo?mobno="+$scope.user.phonenum, {headers: {'X-Mashape-Key': ''}});
    ...
Sign up to request clarification or add additional context in comments.

5 Comments

its not necessary to define $scope.user={}
Can't this prevent problems linked to Scope inheritance ?
what kind of problems?
Let's set this input text in a block with ng-if directive. Ng-if creates a new scope (that herites the Controller's scope), user will be defined in this scope but won't be visible in the controller
well that wont necessarily create a problem. but still it would be a good practice to follow.
1

you can get the value of model in your controller if it is inside the scope of the controler with $scope.ModelName

do this $scope.user.phonenum wherever you want the value of phone number

But check if the model is within the scope of the controller.

Comments

1

with Angular you should change your way of thinking, in the controller you don't work with the view... you work with the model (and let the directives update that model). So, in your example the textbox is mapped to user.phonenum, so you could do something like $scope.user.phonenum in your controller.

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.