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

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


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';}


Vojta described the "Angular way", but if you really need to make this work, @urbanek recently posted a workaround using ng-init:

<input type="text" ng-model="rootFolders" ng-init="rootFolders='Bob'" value="Bob">

https://groups.google.com/d/msg/angular/Hn3eztNHFXw/wk3HyOl9fhcJ


Overriding the input directive does seem to do the job. I made some minor alterations to Dan Hunsaker's code:

  • Added a check for ngModel before trying to use $parse().assign() on fields without a ngModel attributes.
  • Corrected the assign() function param order.
app.directive('input', function ($parse) {  return {    restrict: 'E',    require: '?ngModel',    link: function (scope, element, attrs) {      if (attrs.ngModel && attrs.value) {        $parse(attrs.ngModel).assign(scope, attrs.value);      }    }  };});