angularjsangularjs-scope

[[object Object]] appear on textbox in angularjs


[object Object] is automatically appeared as value in every textbox in my form. What went wrong? It appears after I inserted the attribute name="searchr" in <form> tag.

in search.htm:

<form ng-submit="search()" ng-controller="formcontrol" name="searchr">
<input type="search" placeholder="Search here" id="text_search" ng-model="searchr.text" name="text">

in controller.js:

.controller('SearchCtrl', function($scope, $http) { }

Solution

  • <form name="searchr">
    

    This creates an object of type FormController, and stores it into the scope under the name searchr.

    <input name="text">
    

    This creates an object of type NgModelController and stores it in the FormController's text attribute.

    <input ng-model="searchr.text">
    

    This tells angular that the model of the field (i.e. the text that must be displayed in the field) is searchr.text, but due to the above, searchr.textis the NgModelController object created by angular, which is part of the FormController object created by angular.

    Don't use the same name for the form as the name you use to store the model of the form.