I'm looking way to init model with empty value without ng-init or ugly code in controller (my form model is very huge and contains variable amount of fields).
Please to run sample http://jsfiddle.net/c9Q6Y/
"Serialize" link shows "{}" - empty model if input not touched, and {"name":""} if enter something to imput and clear it.
How to adjust angular to init model fields as empty lines by default?
<div ng-app="">
<form name="myForm" ng-controller="Ctrl">
text: <input name="input" ng-model="user.name">
<div>
<a ng-click="Serialize()">Serialize</a>
</div>
{{serializedUser}}
</form>
</div>
function Ctrl($scope) {
$scope.serializedUser = "";
$scope.user = {};
$scope.Serialize = function() {
$scope.serializedUser = JSON.stringify($scope.user);
}
}
I'm afraid you'd have to do it manually...
$scope.user = {prop1: '', prop2: '', prop3: 0, prop4: [] ...};
You could do something like:
$scope.resetUser = function() {
$scope.user = {prop1: '', prop2: '', prop3: 0, prop4: [] ...};
};
And then at the bottom of your controller:
$scope.resetUser();
Another solution might be to return such a stub object from your factory.