2

I have a form with two input fields (session.email and session.psw) bound to the LoginController.session attribute. When I click the reset button, I call the LoginController.reset() function.

I would like make it clear the session attribute, utilizing the variable sessionDefault (empty). However it works just one time, if I reset two times the form, sessionDefault is undefined.

How could I make it as a constant attribute of the controller?

  app.controller('LoginController', function ($scope)
        {
         this.session={};
         var sessionDefault=
              {
                  email : "",
                  psw: ""
              };    

         this.reset = function()
                {  this.session = sessionDefault;  };                       
        });
3
  • it is not an answer, it doesn't work. It is just my not-working code Commented Apr 16, 2015 at 13:29
  • The comments I commented about got removed, sorry Commented Apr 16, 2015 at 13:41
  • oh.. ok, never mind :) Commented Apr 16, 2015 at 13:52

2 Answers 2

2

Try out this out

for reset function just reset it with sessionDefault copy like as shown below

vm.reset = function () {
    vm.session = angular.copy(sessionDefault);
};

here this refers to the controller instance

Notice that I use var vm = this; and then I decorate vm with the members that should be exposed and data-bindable to to the View. vm simply denotes view modal

This does 3 things for me.

  • Provides a consistent and readable method of creating bindings in my controllers

  • Removes any issues of dealing with this scoping or binding (i.e. closures in nested functions)

  • Removes $scope from the controller unless I explicitly need it for something else

Working Demo

script

   var app = angular.module('myApp', []);
   app.controller('LoginController', function ($scope) {
     var vm = this;
     vm.session = {};
     var sessionDefault = {
        email: "",
        psw: ""
     };
     vm.reset = function () {
         vm.session = angular.copy(sessionDefault);
     };
});

html

<div ng-app='myApp' ng-controller="LoginController as login">
    Email:<input type="text" ng-model="login.session.email"/>{{login.session.email}}
    <br>
    Psw:<input type="text" ng-model="login.session.psw"/>{{login.session.psw}}
    <br>
    <button ng-click="login.reset()">Reset</button>
</div>

Take a look at this beautiful stuff.

AngularJS’s Controller As and the vm Variable

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

4 Comments

this code works as mine, it resets just one time. At the second resent, it doesn't work anymore
@DeLac I have updated my answer, check that working demo
now it works! great... thank you. So I need vm.session = angular.copy(sessionDefault) instead of vm.session = sessionDefault I wonder to know why.. I didn't know this function.
@DeLac angular.copy is a kind of deep copy feature provided by angularjs
0
this.reset = function()
                {  this.session = sessionDefault;  };  

The this in this context refers to the function (reset). If you want to access the 'original' this you need to store it in a variable.

 app.controller('LoginController', function ($scope)
        {
         this.session={};
         var sessionDefault=
              {
                  email : "",
                  psw: ""
              };    

         var self = this;
         this.reset = function()
                {  self.session = angular.clone( sessionDefault);  };                       
        });

2 Comments

it makes sense. however, as other solutions, it works just one time. I can't figure out why..
according to @Nidhish, it seems that angular.copy(sessionDefault) is needed

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.