AngularJS - Value attribute on an input text box is ignored when there is a ng-model used?

HelloWorld picture HelloWorld · May 16, 2012 · Viewed 407.8k times · Source

Using AngularJS if I set a simple input text box value to something like "bob" below. The value does not display if the ng-model attribute is added.

    <input type="text"
           id="rootFolder"
           ng-model="rootFolders"
           disabled="disabled"
           value="Bob"
           size="40"/>

Anyone know of a simple work around to default this input to something and keep the ng-model? I tried to use a ng-bind with the default value but that seems not to work either.

Answer

Vojta picture Vojta · May 16, 2012

That's desired behavior, you should define the model in the controller, not in the view.

<div ng-controller="Main">
  <input type="text" ng-model="rootFolders">
</div>


function Main($scope) {
  $scope.rootFolders = 'bob';
}