Pirzada
Pirzada

Reputation: 4713

Clearing input after form submission in angularjs

I have some confusion when clearing input after form submission. Code below.

HTML :

<form novalidate method="POST">
   <input ng-model="person.firstName" type="text" id="firstName" />
<button ng-click="edit(person)" name="saveBtn" >{{text}}</button>

Controller :

app.controller('PersonCtrl', function ($scope, $routeParams, personService) {

    $scope.init = function () {
       $scope.person = {};
    };

    $scope.edit = function (person) {
        personService.insertPerson(person.firstName);
        person.firstName = "";
    };

    $scope.init();
});

Question: if I use person.firstName = ""; OR $scope.person.firstName = ""; both have same behaviour and clear input fine. How it knows about person.firstName in html where I am not mentioning any scope*?*. Don't know if this is valid question but whenever I need to interact with html binding value than need to use $scope.

Upvotes: 0

Views: 941

Answers (1)

joakimbl
joakimbl

Reputation: 18081

It's because $scope.person and person points to the same object in your $scope.edit function ($scope.person === person is true).

Upvotes: 1

Related Questions