1

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 AngularJS 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);
    }
}
4
  • Your fiddle link is wrong. Commented Mar 13, 2013 at 10:28
  • Stewie, thanks for report. Fixed. Commented Mar 13, 2013 at 11:26
  • Why would you need empty models. What are you trying to achieve? Commented Mar 13, 2013 at 14:07
  • I want to process model object after angular, so I need to know what fields object contains even if it is empty. Commented Mar 13, 2013 at 17:28

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.