Rahul Vikram Singh
Rahul Vikram Singh

Reputation: 1

AngularJS ng-model binding with numerical value

In angularJs ng-model i m binding with some name with ++ operator like

<div ng-app="myApp" ng-controller="myCtrl">
    <input type="text" ng-model="firstname++hkjnh">
    {{firstname+" "+lastname}}
</div>

output -> FirstName0 from where 0 comes in text

or

  <div ng-app="myApp" ng-controller="myCtrl">
        <input type="text" ng-model="+124343">
        {{firstname+" "+lastname}}
    </div>

output ->124343

so plese tell me how angular work with these type of things

Upvotes: 0

Views: 69

Answers (2)

Omprakash Sharma
Omprakash Sharma

Reputation: 429

ng-model="firstname++hkjnh" trying to create an variable with controller as $scope.firstname++hkjnh, since in js variable name can't hold any type of operator it will generate an error.

here ++ operator trying to evaluate with RHS value which is undefined,hence model value will be uncertain(varies on machine to machine).

In 2nd case model just evaluate the value(only with numeric value).

Upvotes: 0

user4874420
user4874420

Reputation:

ng-model directive we are using for two way binding. That's just a variable, we can get corresponding value in controller. Refer

https://plnkr.co/edit/AEblZEuiEL8mzvsFoz8M?p=preview

angular.module('inputExample', [])
.controller('ExampleController', ['$scope', function($scope) {
   $scope.val++a = '1';
}]);

<input ng-model="val++a" ng-pattern="/^\d+$/" name="anim" class="my-input"
     aria-describedby="inputDescription" />

Upvotes: 1

Related Questions