Input default value

15,798 views
Skip to first unread message

ganaraj p r

unread,
Jun 12, 2012, 10:21:09 AM6/12/12
to ang...@googlegroups.com
Has anyone created a directive that sets the default value of an input field?

It seems like ngModel overwrites whatever is there in the value field.. Ideally I would want it to overwrite if there is some value in ngModel.. and not overwrite when the model is undefined or blank!

--
Regards,
Ganaraj P R

Marcello Nuccio

unread,
Jun 13, 2012, 5:56:24 AM6/13/12
to ang...@googlegroups.com
Putting default values in the view, makes testing harder. The Angular way of setting defaults, is by setting them in the $scope.

urbanek....@gmail.com

unread,
Sep 26, 2012, 4:18:31 AM9/26/12
to ang...@googlegroups.com
Hi,

unfortunately, there are cases where the form should have default values ​​as input [type = "hidden"]. Similar is like validation - for example: ngMinlength / ngMaxlength / ngPattern, which are also on the element and not defined in the $scope.

Poor but functional process can be view as follows:

<input type="hidden" ng-init="type ='type-1'" ng-model="type" value="type-1" />


JU

Rahul Semwal

unread,
May 19, 2017, 11:46:03 PM5/19/17
to Angular and AngularJS discussion, urbanek....@gmail.com
Hi,

I also uses the ng-init for the same problem. I know it is a poor approach but if there is any kind of standard approach will be there so please share with us. But I tried a lot to sync ng-value with ng-model but it would not work for me.

Sander Elias

unread,
May 20, 2017, 12:09:11 AM5/20/17
to Angular and AngularJS discussion, urbanek....@gmail.com
Hi Rahul,

This is highly dependent on where you get your default values? 
If you call those values in by $http, you can better provide defaults from within your controller. If it's injected into the template by your server, ngInit is the way to go. (BTW, this approach has its own share of issues!)

Regards
Sander

Rahul Semwal

unread,
May 22, 2017, 2:39:38 AM5/22/17
to Angular and AngularJS discussion, urbanek....@gmail.com
Hi Sander,

yes I totally agreed with you.This is the last and final solution for me to initialize ng-model with default value inside controller (I am fetching default values from $http req). But I was wondering a simple trick to do it on view only. Thanks Sander for giving me clear cut idea.

Regards,
Rahul Semwal
Reply all
Reply to author
Forward
0 new messages