在范围内使用值名称NG-模式? [英] Use value in Scope to name ng-model?

查看:129
本文介绍了在范围内使用值名称NG-模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个对象,看起来像这样:

Assume I have an object that looks like this:

$scope.obj.foo = 'bar';

我想输入场有NG-模式='酒吧'

I want an input-field to have the ng-model='bar'

如果我定义它是这样的:

If I define it like this:

<input ng-model="{{obj.foo}}" />

我得到一个JavaScript错误

I get a javascript error

如果我定义它是这样的:

If I define it like this:

<input ng-model="obj.foo" />

在NG-模型obj.foo和输入元素得到的填充值吧。

the ng-model is obj.foo and the input-element get's populated with the value bar.

如何我应该写我的模板,以实现一个输入字段是这样的:
期望的结果:

How should I write my template in order to achieve an input-field that works like this: Desired outcome:

<input ng-model="bar" />

但取出由范围NG-模型的名称,而不是写它到模板。

but fetching the ng-model-name from the scope rather than writing it into the template.

推荐答案

您有需要用到的对象模型,所以你可以这样做

You need to use an object anyway for the model, so you can do like this

<input ng-model="obj[myModelName]" />

,然后在插 myModelName 与任foo的

这篇关于在范围内使用值名称NG-模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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