1

I have an input text field as :

<input type="text" class="form-control" id="inputValue" name="uservalue" ng-model="inputNumber" ng-required="true" autofocus="true" ng-blur="checkIfValid()"/>

My controller has the ng-blur function as follows:

$scope.checkIfValid = function(){
        console.log("Value is = " + $scope.inputNumber);
}

I have defined - $scope.inputNumber = '';

My console.log shows an empty value even though I input a value in the text field and hit 'Tab' or move on to the next field to invoke the ng-blur function.

What am I doing wrong here? `

8
  • 2
    Possible plunker ? Commented Aug 19, 2016 at 6:23
  • try with ng-change instead ng-blur for debug purposes. also +1 for a plunker detailing the problem. Commented Aug 19, 2016 at 6:24
  • agreed on a plunker - how are the controller/directives tied together? Commented Aug 19, 2016 at 6:27
  • Does your console displays Value is = or does it not display anything? Commented Aug 19, 2016 at 6:28
  • 1
    1) Did you by any chance forget to add ng-app and ng-controller ? 2) Change ng-blur to ng-change. 3) add a $watch for inputValue and see if it displays anything in the log Commented Aug 19, 2016 at 6:44

1 Answer 1

3

Check this

var app = angular.module("myapp", []);
app.controller("myCtrl", ['$scope', function($scope) {
    $scope.inputNumber="new";
  
  $scope.checkIfValid = function(){
        console.log("Value is = " + $scope.inputNumber);
}
  
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp">  
  <div ng-controller="myCtrl">
   <input type="text" class="form-control" id="inputValue" name="uservalue" ng-model="inputNumber" ng-required="true" autofocus="true" ng-blur="checkIfValid()"/>
    
    <h1>{{inputNumber}}</h1>
  </div>
</div>

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

3 Comments

Even though your answer is correct - this is what worked for me : I passed inputNumber as a parameter to checkifValid(inputNumber) and then the value enters my controller, any idea why this is happening?
Hi, i didnot get , you want to pass value as parameter
Yeah - I passed the ng-model as a parameter , and that worked - I can get the value now in my controller .. but $scope.inputNumber doesn't seem to be binding to the value in input in the text box - any idea why?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.