Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular model doesn't update with space

When an angular model is bound to an input, Angular doesn't seem to update the value if a space is added. Even if the model is watched, the value still does not update.

I created a JS Fiddle to demonstrate the issue. Type a string, and notice the values in the bound spans update. However, add a space to the end of the string and the value doesn't update. Is there a way to force angular to watch for spaces as well?

The specific code is:

View

<div ng-controller="MyCtrl">
  <input data-ng-model="inputValue">
  <p>This value: ----<span data-ng-bind="inputValue"></span>----</p>
</div>

Controller

function MyCtrl($scope) {
  $scope.inputValue = 'Superhero';
});
like image 456
Eric Di Bari Avatar asked Feb 18 '14 05:02

Eric Di Bari


1 Answers

You need to set ngTrim to false. By default Angular sets it to true, which trims white space in input boxes:

<input data-ng-model="inputValue" data-ng-trim="false" />

Fiddle: http://jsfiddle.net/vYLQk/9/

Docs: http://code.angularjs.org/1.2.13/docs/api/ng.directive:input.text

like image 120
Anthony Chu Avatar answered Oct 05 '22 04:10

Anthony Chu