2

I'm just normally using an input tag and I wanted to insert some text already with the value tag but it just won't work, this is my code line:

<input class="form-control input-sm" value="14" type="number" ng-model="textLetterGrootte">

As far as I can see I'm not doing anything wrong.

0

2 Answers 2

7

If you use AngularJs ngModel directive, remember that the value of value attribute does not bind on ngModel field.You have to init it by yourself and the best way to do it,is DEMO

<input class="form-control input-sm" type="number" ng-model="textLetterGrootte"  ng-init="textLetterGrootte=14">
Sign up to request clarification or add additional context in comments.

7 Comments

Please explain why this work in order to help other people. What does ng-init and why with just the value attr it's not enough.
How do I use ng-init when I want to display text instead of numbers ? <input class="form-control input-sm" type="text" ng-model="textKleur">
<input class="form-control input-sm" type="text" ng-model="textKleur" ng-init="textKleur='SOMETEXT'"/>
@SVK Sorry, ng-init="textKleur="SOMETEXT"" doesn't seem to work.
use single quotation .ng-init="textKleur='SOMETEXT' " @Goldenowner
|
1

Even though you have accepted the answer, let me put it so you will understood it in depth. The official documentation says ng-init will take any expression. So yes, you can do what you want above as long as the associated scope defines a function called init() or assigning the initial value directly.

This directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit, such as for aliasing special properties of ngRepeat, as seen in the demo below; and for injecting data via server side scripting. Besides these few cases, you should use controllers rather than ngInit to initialize values on a scope.

Thus, as you can simply initialize your variables when the controller is created. So there's really no need to use ng-init at all. For example:

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

 myApp.controller('myController', ['$scope', function($scope){

  $scope.init = function() {
      $scope.textLetterGrootte = 14;
  };
  // runs once per controller instantiation
  $scope.init();
}]);

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.