admin管理员组

文章数量:1194180

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

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';
});
Share Improve this question asked Feb 18, 2014 at 5:04 Eric Di BariEric Di Bari 3,8677 gold badges42 silver badges49 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 29

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

本文标签: javascriptAngular model doesn39t update with spaceStack Overflow