3
<form class="form-horizontal" role="form" name="addCreditoBuscar" id="addCreditoBuscar" ng-controller="AddCreditoAppController">
 <div class="form-group">
  <label class="col-sm-2 col-sm-2 control-label">Buscar</label>
   <div class="col-sm-10">
    <input type="text" class="form-control" name="buscar" ng-model="addCreditoBuscar.buscar" ng-required="true" placeholder="Buscar por cedula, nombre o apellido">
    <span class="help-block" ng-show="addCreditoBuscar.buscar.$error.required">Este campo es requerido es requerido.</span>
   </div>
  </div>
</form>

This is my code for a input text, but the result is this one - http://prntscr.com/73otmc

3
  • 4
    You are getting this as addCreditoBuscar.buscar is an object. Commented May 10, 2015 at 16:35
  • possible duplicate of Access / process (nested) objects, arrays or JSON Commented May 10, 2015 at 16:52
  • put this beside input and see exactly what object looks like in the view <pre>{{addCreditoBuscar.buscar | json}}</pre> Commented May 10, 2015 at 16:55

3 Answers 3

7

The solution is simple. You are using the same "form name" than the object you want to put. Change the "Form name" to one different like name="formAddCreditoBuscar"

<form class="form-horizontal" role="form" name="formAddCreditoBuscar" id="addCreditoBuscar" ng-controller="AddCreditoAppController">

Then your problem would disappear

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

2 Comments

Best answer here.
Ahh, had this due to silly copy-paste errors. Adapted the [ngModel] linking, but forgot the name.
1

Your input element is coercing the value you provided to a string. Your value is an object, so it is coercing it to the string value of that object ([[object Object]]).

To fix this, you need to access a property of that object.

ng-model="addCreditoBuscar.buscar" is an object, therefore it will show up as [[object Object]], as something like Object.prototype.toString() is acting on the object in order to coerce it to a string value in your input element.

For example:

var obj = {
    someProp: 'someVal'
};

console.log(''+a); // => "[object Object]"

The above demonstrates string coercion from an object.

If you would like to show a property from that object, simply use the object attribute accessor (the dot .):

addCreditoBuscar.buscar.whateverProperty

Therefore, whenever your input element coerces the property to a value, if the value is a string, it will show up as a string (which is your intended output):

console.log(''+a.someProp); // => "someVal"

Comments

0

You could erase the displayed value by explicitly change the HTMLElement.value property at every change of your model value. You can create a directive to handle that.

app.directive('labelField', function () {
    return {
        restrict: 'AE',
        link: function ($scope, element, attrs) {
            $scope.$watch(attrs['ngModel'], function (newValue) {
                if (newValue) {
                    element[0].value = newValue[attrs['labelField']];
                }
            })
        }
    }
})

use it by passing the field to display in the directive

<input type="text" ng-model="myobject" label-field="myfield">

E.g. to store in $scope.user an object like {name:"John","email":"[email protected]"}, you would use it like that :

<input type="text" ng-model="user" label-field="name">

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.