mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-04-30 19:34:42 +00:00
feat(ngModel): support ngTrim attribute on input
This commit is contained in:
parent
4909d1d39d
commit
d519953a4b
2 changed files with 26 additions and 3 deletions
|
|
@ -25,6 +25,8 @@ var inputType = {
|
||||||
* patterns defined as scope expressions.
|
* patterns defined as scope expressions.
|
||||||
* @param {string=} ngChange Angular expression to be executed when input changes due to user
|
* @param {string=} ngChange Angular expression to be executed when input changes due to user
|
||||||
* interaction with the input element.
|
* interaction with the input element.
|
||||||
|
* @param {boolean=} [ngTrim=true] If set to false Angular will not automatically trimming the
|
||||||
|
* input.
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
<doc:example>
|
<doc:example>
|
||||||
|
|
@ -32,12 +34,12 @@ var inputType = {
|
||||||
<script>
|
<script>
|
||||||
function Ctrl($scope) {
|
function Ctrl($scope) {
|
||||||
$scope.text = 'guest';
|
$scope.text = 'guest';
|
||||||
$scope.word = /^\w*$/;
|
$scope.word = /^\s*\w*\s*$/;
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
<form name="myForm" ng-controller="Ctrl">
|
<form name="myForm" ng-controller="Ctrl">
|
||||||
Single word: <input type="text" name="input" ng-model="text"
|
Single word: <input type="text" name="input" ng-model="text"
|
||||||
ng-pattern="word" required>
|
ng-pattern="word" required ng-trim="false">
|
||||||
<span class="error" ng-show="myForm.input.$error.required">
|
<span class="error" ng-show="myForm.input.$error.required">
|
||||||
Required!</span>
|
Required!</span>
|
||||||
<span class="error" ng-show="myForm.input.$error.pattern">
|
<span class="error" ng-show="myForm.input.$error.pattern">
|
||||||
|
|
@ -66,6 +68,12 @@ var inputType = {
|
||||||
input('text').enter('hello world');
|
input('text').enter('hello world');
|
||||||
expect(binding('myForm.input.$valid')).toEqual('false');
|
expect(binding('myForm.input.$valid')).toEqual('false');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should not be trimmed', function() {
|
||||||
|
input('text').enter('untrimmed ');
|
||||||
|
expect(binding('text')).toEqual('untrimmed ');
|
||||||
|
expect(binding('myForm.input.$valid')).toEqual('true');
|
||||||
|
});
|
||||||
</doc:scenario>
|
</doc:scenario>
|
||||||
</doc:example>
|
</doc:example>
|
||||||
*/
|
*/
|
||||||
|
|
@ -370,7 +378,14 @@ function isEmpty(value) {
|
||||||
function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
|
function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
|
||||||
|
|
||||||
var listener = function() {
|
var listener = function() {
|
||||||
var value = trim(element.val());
|
var value = element.val();
|
||||||
|
|
||||||
|
// By default we will trim the value
|
||||||
|
// If the attribute ng-trim exists we will avoid trimming
|
||||||
|
// e.g. <input ng-model="foo" ng-trim="false">
|
||||||
|
if (toBoolean(attr.ngTrim || 'T')) {
|
||||||
|
value = trim(value);
|
||||||
|
}
|
||||||
|
|
||||||
if (ctrl.$viewValue !== value) {
|
if (ctrl.$viewValue !== value) {
|
||||||
scope.$apply(function() {
|
scope.$apply(function() {
|
||||||
|
|
|
||||||
|
|
@ -382,6 +382,14 @@ describe('input', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should update the model and not trim the value', function() {
|
||||||
|
compileInput('<input type="text" ng-model="name" name="alias" ng-trim="false" />');
|
||||||
|
|
||||||
|
changeInputValueTo(' a ');
|
||||||
|
expect(scope.name).toEqual(' a ');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should allow complex reference binding', function() {
|
it('should allow complex reference binding', function() {
|
||||||
compileInput('<input type="text" ng-model="obj[\'abc\'].name"/>');
|
compileInput('<input type="text" ng-model="obj[\'abc\'].name"/>');
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue