Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS default value for ng-model

Tags:

angularjs

Is it possible to make ng-models get default values, like ''?

I have a form on which I have used jQuery's serialize function, and whenever a a value is not present, it will still include it in the serialized data, E.g. {name: '', age: ''}.

However, when I use try using posting it by using Angular's $http and getting the model from $scope, it appears as undefined when it is empty.

like image 294
whirlwin Avatar asked Jul 01 '13 22:07

whirlwin


1 Answers

You may also try:

<div ng-init="foo={}">
    <input type="text" ng-model="foo.name=''"/>
</div>

or:

<input type="text" ng-init="foo.name=''"/>

it will present:

foo = { name: '', .... }
like image 103
Neaped Avatar answered Oct 04 '22 21:10

Neaped