AngularJS default value for ng-model

angularjs

Solution

You can simply define the properties on the model in the scope in advance of using them in your view.

If you show your controller code I'll show you what you need to update to add them to the scope.

It should look something like:

In your js file that defined the app

angular.module("MyModule", []).controller('MyCtrl', ['$scope', function ($scope) {
    $scope.myModel = {name:""};
}]);

In your HTML

<input type="text" ng-model="myModel.name"/>

Note I prefer not having global variables/functions so I'm using a different syntax provided by Angular to avoid this and to allow for minification.

Problem

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.

Original source

Related problems