2

I have a strange situation in which $scope variables binding do not appear to be work as expected.

Here is the HTML:

        <div class="input-group" style="width:100px">
            <input type="number" 
                   class="form-control" 
                   id="Sampling_Request_for_Current_Sampling_INPUT"
                   ng-model="aabbcc" 
                   style="width:125px;text-align:center">
            <span class="input-group-btn">
                <button class="btn btn-default" ng-disabled="Cannot_Allocate_Yet" ng-click="Get_Sampling_Request_Details()" type="button">{{All_Labels.Common.Display}}</button>
            </span>
        </div>

and here is the scope function invoked upon clicking on the button:

$scope.Get_Sampling_Request_Details = function () {
    console.log("$scope.aabbcc: " + $scope.aabbcc) ;
}

The variable $scope.aabbcc is initialized to 0 upon controller's loading.

Regardless what I type into the input element, I always get 0 in the console.

6
  • 1
    Can you show your full controller code? Commented May 16, 2017 at 9:15
  • Try passing aabbcc to your function and accessing that parameter in your js file Commented May 16, 2017 at 9:17
  • Thank you @iamalismith for your prompt reply. Unfortunately, company policy does not allow that. The interesting point is that the same page has other inputs connected to scope variables that would OK. This one, however, does not for some reason. Commented May 16, 2017 at 9:20
  • Maybe somewhere your model is getting changed. Commented May 16, 2017 at 9:21
  • @RishabhJain, that would be a bypass of the issue. variables declared using ng-model SOULD be updated and properly seen within the JS. Commented May 16, 2017 at 9:21

4 Answers 4

9

This scenario generally happens, If you have wrapped your HTML inside ng-if, ng-switch ng-repeat.. or some other directive that creates new child scope. See this fiddle.

So it's a best practice to wrap your scope in some model to leverage protypical inheritance and correctly bind data to $scope.

Like : $scope.data.aabbcc = 0 and use it like ng-model ='data.aabbcc'.

See this for few minutes and Read this for complete understanding.

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

3 Comments

This seems like the most likely explanation to me
BINGO!!!!!!!!!!!!!!!!!!!!!!! Thank you. I would say that AngularJS should have a better "book keeping" mechanism... Still, great library. Thanks again @anoop!!!
glad to help you :) , See this for few minutes and Read this for complete understanding.
1

check this working example

<div ng-controller="MyCtrl">
Hello, {{name}}!
 <input type="number" ng-model="name"/>
 <button class="btn btn-default" ng-disabled="Cannot_Allocate_Yet" ng-
 click="Get_Sampling_Request_Details()" type="button">test</button>
</div>

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


 function MyCtrl($scope) {
    $scope.name = 0;
         $scope.Get_Sampling_Request_Details = function () {
console.log("$scope.aabbcc: " + $scope.name) ;
 }
 }

1 Comment

... and I have quite many examples in the same project that work smoothly. Thanks anyway.
0

AngularJS controllers control the data. The scope is the binding part between the HTML (view) and the JavaScript (controller). You must define the ng-model inside a ng-controller within which its scope lies. Try this out.

<div ng-controller="myCtrl">
<div class="input-group" style="width:100px">
        <input type="number" 
               class="form-control" 
               id="Sampling_Request_for_Current_Sampling_INPUT"
               ng-model="aabbcc" 
               style="width:125px;text-align:center">
        <span class="input-group-btn">
            <button class="btn btn-default" ng-disabled="Cannot_Allocate_Yet" ng-click="Get_Sampling_Request_Details()" type="button">{{All_Labels.Common.Display}}</button>
        </span>
</div>
</div>
<script>
var app = angular.module('Myapp', []);
app.controller('myCtrl', function($scope) {
    $scope.Get_Sampling_Request_Details = function () {
    console.log("$scope.aabbcc: " + $scope.aabbcc) ;
} 
});
</script>

1 Comment

As stated in one of my comments, there are plenty examples in my project that work perfectly well. Thanks for your answer anyway.
0

Declare an empty object in your controller section .

eg: $scope.obj = {};

And use like ng-model="obj.key_name" in your html. It will work.

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.