-3

[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) { }
4
  • sounds to me like you may be binding objects to the textboxes instead of strings. What does your html & controller code look like? Commented Apr 18, 2015 at 16:22
  • 2
    We can't debug code without seeing any line of it. Commented Apr 18, 2015 at 16:22
  • It's probably something to do with your ng-model="searchr.text". What is searchr? I see it in the name attribute of your form, but do you have a js object by that name? Commented Apr 18, 2015 at 16:35
  • I also don't think that's the right controller that goes with that form. Do you have a controller by the name FormControl, like the ng-controller attribute refers to? Commented Apr 18, 2015 at 16:38

1 Answer 1

2
<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.

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

1 Comment

This answer pointed me into the right direction solving my problem where I accidentally used the same name for different input-tags in a very large form. When submitting/saving the object, one input field changed value to "[object Object]" but I could not log this value to the console anywhere. Instead I got the value I expected when debugging. I use Angular 8 in this case. Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.