0

I wish to know what is the purpose of $scope.reset(); in the controller function? The reset seems to work without this code as well but documentation says to use this code and I can't understand why:

 <body ng-app="myapp" ng-controller="resetController">
    <label>Enter name</label><input type="text" ng-model="name"/>
    <label>Enter emailid</label><input type="text" ng-model="email"/>
    <button ng-click="reset()">Reset</button>
    <script>
        angular.module("myapp",[])
        .controller("resetController", function($scope)
        {
            $scope.reset = function()
            {
                $scope.name = "";
                $scope.email = "";
            }
            $scope.reset(); /* not sure why we need this */
        });
    </script>
</body>
5
  • reset will clear the input fields that name and email are bound to Commented Dec 31, 2015 at 8:54
  • but the input fields are cleared even if I omit the code $scope.reset(); from the controller @pixelbits . So what is its purpose then? Commented Dec 31, 2015 at 8:56
  • You're right, its redundant in this case. Commented Dec 31, 2015 at 8:58
  • any thoughts on when such coding could actually be useful @pixelbits? Commented Dec 31, 2015 at 8:59
  • i think a button with type="reset" will solve the issue, no need to have all that angular code. :) Commented Dec 31, 2015 at 9:39

3 Answers 3

2
<body ng-app="myapp" ng-controller="resetController">
        <label>Enter name</label><input type="text" ng-model="name"/>
        <label>Enter emailid</label><input type="text" ng-model="email"/>
        <button ng-click="reset()">Reset</button>
    <script>
        angular.module("myapp",[])
        .controller("resetController", function($scope)
        {
            $scope.reset = function()
            {
                $scope.name = "";
                $scope.email = "";
            }
            $scope.reset(); /* not sure why we need this */
        });
    </script>
</body>

This is your code. Now see, You have ng-click="reset()". This is calling $scope.reset method inside controller hence fields are getting cleared.

As you want to know that: $scope.reset(); /* not sure why we need this */

NO, you don't need this, unless you are not calling this method from your view. $scope.reset() is used inside controller, when you need to call method on page load event.

* I think $scope.reset() may be used to clear the fields when page is loaded first time.

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

1 Comment

You are correct. Adding an alert() to the reset function proves what you said. Cheers.
1

you ask /* not sure why we need this */

 $scope.reset = function(){
            $scope.name = "";
            $scope.email = "";
 }
 $scope.reset(); /* not sure why we need this */

In fact it call the function reset() the first time the controller is instantiated, so before you have the time to put anythink on the two input. It is better to declare fields before you use them to avoid "undefined" error in controller side. in this particular case it change nothing but maybe in the example they do another thing more.

You can try to put an alert inside the function, it should call it on first run, and on each time you click on reset()

 $scope.reset = function(){
            alert("call to reset");
            $scope.name = "";
            $scope.email = "";
 }

1 Comment

Thanks a lot. Triggering an alert() explains everything.
0

Try this way

<body ng-app="myapp" ng-controller="resetController">
<label>Enter name</label><input type="text" ng-model="fromData.name"/>
<label>Enter emailid</label><input type="text" ng-model="fromData.email"/>
<button ng-click="reset()">Reset</button>
<script>
    angular.module("myapp",[])
    .controller("resetController", function($scope)
    {
        $scope.fromData = [];


        $scope.reset = function()
        {
            $scope.fromData = [];
        }
    });
</script>

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.