简体   繁体   English

AngularJS:绑定到所有对象属性?

[英]AngularJS: Bind to all object properties?

I have a Model that takes an object that could contain any set of properties. 我有一个Model,它接受一个可以包含任何属性集的对象。

I am wondering if I can bind to each object property, without knowing the property values ahead of time. 我想知道我是否可以绑定到每个对象属性,而不提前知道属性值。

Take this for example 以此为例

var object1 = 
{
    Name: '1',
    Identifier: '12345',
    Password: 'password'
}

var object2  =
{
    Name: '2',
    Identifier: 'object_two'
}

var objects = [object1,object2];

My objects can have different and unique properties, and I'd like to bind this to a form. 我的对象可以具有不同且唯一的属性,我想将其绑定到表单。

I know I can use a for (var property in object) and iterate through each object -- But can I do the same thing with AngularJS binding? 我知道我可以使用for(对象中的var属性)并遍历每个对象 - 但我可以使用AngularJS绑定做同样的事情吗?

I wanted to do something along the lines of: 我想做一些事情:

<ul><li ng-repeat="attrib in obj">
                {{attrib}}
            </li</ul>

Edit: Thanks to the answer below I have gotten a little further. 编辑:感谢下面的回答,我进一步了解。 I can't seem to achieve two-way binding with this. 我似乎无法与此实现双向绑定。 This fiddle illustrates what I am trying to do: 这个小提琴说明了我要做的事情:

http://jsfiddle.net/EpqMc/17/ http://jsfiddle.net/EpqMc/17/

I essentially want to bind using something like this: 我基本上想要使用这样的东西绑定:

<p ng-repeat="(key,value) in obj">                    
                    {{key}} : <input ng-model="obj[key]" />

                </p>

This works, except that I can only enter one character at a time -- interesting. 这是有效的,除了我一次只能输入一个字符 - 有趣。

Final Edit: I found an acceptable alternative in case anyone else has this problem. 最终编辑:我找到了一个可接受的替代方案,以防其他人遇到此问题。 I wasn't able to bind directly to an object without some issues, but i WAS able to bind to an array like the docs state in the answer below. 我没有能力直接绑定到一个对象没有一些问题,但我能够绑定到像下面的答案中的文档状态的数组。

The following fiddle accomplishes what I need to do, while being only slightly more verbose. 以下小提琴完成了我需要做的事情,而只是稍微冗长一点。

http://jsfiddle.net/8ENnx/9/ http://jsfiddle.net/8ENnx/9/

function ctrl($scope) {
    $scope.objects =
                [
                [
                    {Name: 'Name:', Value: 'Joe'},
                    {Name: 'Identification', Value: '1'},
                    {Name: 'Password', Value: 'secret'}
                ],
                    [
                    {Name: 'Name:', Value: 'Jane'},
                    {Name: 'Identification', Value: '2'},
                    {Name: 'Weather', Value: 'Sunny'}
                ]
                ];

//    $scope.setSelected = ?????;

}

<div ng-app>
    <div ng-controller="ctrl">
        Objects
        <ul>
              <br/>
                Listing of properties:
                <br/>

            <li ng-repeat="obj in objects" class="swatch">                               
                {{obj}}
                <p ng-repeat="prop in obj">
                    {{prop.Name}}: <input ng-model="prop.Value" /></p>


            </li>
        </ul>

    </div>
</div>

This allows me to define an arbitrary set of arguments, but still bind without issues using angular. 这允许我定义一组任意参数,但仍然使用angular绑定没有问题。

No problem: 没问题:

<li ng-repeat='(key, value) in obj'>
    {{key}} : {{value}}
</li>

It's in the docs as well: http://docs.angularjs.org/api/ng.directive:ngRepeat 它也在文档中: http//docs.angularjs.org/api/ng.directivengRepeat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM