在命名表单上使用ng-model时获取[Object object] [英] Getting [Object object] when using ng-model on a named form

查看:172
本文介绍了在命名表单上使用ng-model时获取[Object object]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下条件:

<form
    id="registration"
    name="registration"
    method="POST"
>

    <input
        type="text"
        name="first_name"
        id="first-name"
        ng-model="registration.first_name"
        placeholder="First name"
    />

当我的表单显示时,配置为上面的字段最终有 [Object object ] 其中的初始值。

When my form displays, fields configured as above end up having [Object object] inside of them for their initial value.

我在这里做错了什么,什么是正确的方法来获取双向绑定一个表单?

What am I doing wrong here and what's the correct way to get two-way binding inside of a form?

推荐答案

在表单上设置名称属性将创建一个范围对象,它是有用的验证,但不是用于 ng-model 输入的属性。

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.

如果您为ng-model使用单独的范围变量,它将按照您期望的方式工作: p>

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"
    ...
/>

演示

这篇关于在命名表单上使用ng-model时获取[Object object]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆