0

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.

1 Answer 1

1

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

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.