Getting [Object object] when using ng-model on a named form

后端 未结 4 1138
孤街浪徒
孤街浪徒 2021-02-14 07:12

If I have the following:

相关标签:
4条回答
  • 2021-02-14 07:29

    I guess either the registration scope is not initialized or registration.first_name is an object.

    Have a look at this plunker: http://plnkr.co/edit/bdbs0XVB8hZSodOTYyM4?p=preview

    Initialize the registation model either in controller or using ng-init ng-init="registration = {}"

    If registration.first_name is an object then this will be displayed as [Object object]. You have to supply a proper string value as model to the ng-model

    0 讨论(0)
  • 2021-02-14 07:35

    Angularjs Form showing [object Object] on load in form fields

    I am guessing, it will show this error, only if your form name and ng-model name (value) are same

    like
    
     <form name="userform">-------
     <input ng-model="userform.firstname">
    

    change your form name or your ng-model value to user or some xyz

    0 讨论(0)
  • 2021-02-14 07:40

    Setting the name attribute on a form creates a scope object which is useful for validation but is not meant to be used for the ng-model attributes of inputs.

    If you use a separate scope variable for ng-model, it will work as you expect:

    <form
        ...
        name="reg"
        ...
    >
    
    <input
        ...
        ng-model="registration.first_name"
        ...
    />
    

    Demo

    0 讨论(0)
  • 2021-02-14 07:42

    If you don't register a controller in ng-controller, you can remove name="registration" from form, because controller has init with forms, i try and works.

    plnkr.co/edit/JK3utpe1eERrJeoveaCQ?p=preview

    0 讨论(0)
提交回复
热议问题