mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-23 03:30:02 +08:00
fix(input): always format viewValue as a string inputs with text controls
Backported from 1eda18365a
NgModel will format all scope-based values to string when setting the viewValue for
the associated input element. The formatting, however, only applies to input elements
that contain a text, email, url or blank input type. In the event of a null or undefined
scope or model value, the viewValue will be set to null or undefined instead of being
converted to an empty string.
Closes #5936
Closes #9277
This commit is contained in:
@@ -310,6 +310,87 @@ describe('ngModel', function() {
|
||||
}));
|
||||
|
||||
|
||||
it('should always format the viewValue as a string for a blank input type when the value is present',
|
||||
inject(function($compile, $rootScope, $sniffer) {
|
||||
|
||||
var form = $compile('<form name="form"><input name="field" ng-model="val" /></form>')($rootScope);
|
||||
|
||||
$rootScope.val = 123;
|
||||
$rootScope.$digest();
|
||||
expect($rootScope.form.field.$viewValue).toBe('123');
|
||||
|
||||
$rootScope.val = null;
|
||||
$rootScope.$digest();
|
||||
expect($rootScope.form.field.$viewValue).toBe(null);
|
||||
|
||||
dealoc(form);
|
||||
}));
|
||||
|
||||
|
||||
it('should always format the viewValue as a string for a `text` input type when the value is present',
|
||||
inject(function($compile, $rootScope, $sniffer) {
|
||||
|
||||
var form = $compile('<form name="form"><input type="text" name="field" ng-model="val" /></form>')($rootScope);
|
||||
$rootScope.val = 123;
|
||||
$rootScope.$digest();
|
||||
expect($rootScope.form.field.$viewValue).toBe('123');
|
||||
|
||||
$rootScope.val = null;
|
||||
$rootScope.$digest();
|
||||
expect($rootScope.form.field.$viewValue).toBe(null);
|
||||
|
||||
$rootScope.val = undefined;
|
||||
$rootScope.$digest();
|
||||
expect($rootScope.form.field.$viewValue).toBeUndefined();
|
||||
|
||||
dealoc(form);
|
||||
}));
|
||||
|
||||
|
||||
it('should always format the viewValue as a string for an `email` input type when the value is present',
|
||||
inject(function($compile, $rootScope, $sniffer) {
|
||||
|
||||
var fakeEmail = {};
|
||||
fakeEmail.toString = function() { return 'fake@email'; };
|
||||
var form = $compile('<form name="form"><input type="email" name="field" ng-model="val" /></form>')($rootScope);
|
||||
$rootScope.val = fakeEmail;
|
||||
$rootScope.$digest();
|
||||
expect($rootScope.form.field.$viewValue).toBe('fake@email');
|
||||
|
||||
$rootScope.val = null;
|
||||
$rootScope.$digest();
|
||||
expect($rootScope.form.field.$viewValue).toBe(null);
|
||||
|
||||
$rootScope.val = undefined;
|
||||
$rootScope.$digest();
|
||||
expect($rootScope.form.field.$viewValue).toBeUndefined();
|
||||
|
||||
dealoc(form);
|
||||
}));
|
||||
|
||||
|
||||
it('should always format the viewValue as a string for a `url` input type when the value is present',
|
||||
inject(function($compile, $rootScope, $sniffer) {
|
||||
|
||||
var fakeUrl = {};
|
||||
fakeUrl.toString = function() { return 'https://www.angularjs.org'; };
|
||||
var form = $compile('<form name="form"><input type="url" name="field" ng-model="val" /></form>')($rootScope);
|
||||
$rootScope.val = fakeUrl;
|
||||
$rootScope.$digest();
|
||||
expect($rootScope.form.field.$viewValue).toBe('https://www.angularjs.org');
|
||||
|
||||
$rootScope.val = null;
|
||||
$rootScope.$digest();
|
||||
expect($rootScope.form.field.$viewValue).toBe(null);
|
||||
|
||||
$rootScope.val = undefined;
|
||||
$rootScope.$digest();
|
||||
expect($rootScope.form.field.$viewValue).toBeUndefined();
|
||||
|
||||
dealoc(form);
|
||||
}));
|
||||
|
||||
|
||||
it('should register/deregister a nested ngModel with parent form when entering or leaving DOM',
|
||||
inject(function($compile, $rootScope) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user