1

I am new to angular js. I have an input field of type number. I want to pre-populate it with a value that comes from a json file.

This does not work when I assign the value using ng-model. It works on a regular input of type text though.

I have also tried using ng-value directive and the value attribute. But no luck.

Need help. Thanks!

2 Answers 2

1

Make sure you are using a number in the JSON and not passing it back as a string. For eg. {"numberValue" : 50} and not {"numberValue": "50"}

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

1 Comment

Thanks! That was the problem
0

As the ng-model acts like a two-way data binding, scope->view and view->scope you should assign that initial value within the controller initialization rather than using ng-model or the value attribute within the html markup.

    angular.module('MyApp.controllers',['MyAPP']).controller('MyCtrl', function($scope, $rootScope, $http, $location) {
      // This object will be filled by the form

      $scope.user = {};
      //Prepopulated value
      $scope.user.yearOfBirth=1911;
      ...

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.