角度ng-repeat内部的动态属性引用 [英] dynamic attribute reference inside angular ng-repeat

查看:57
本文介绍了角度ng-repeat内部的动态属性引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个dropdown指令,它可以使用不同的对象类型,每个对象都有自己的特定属性,我需要在下拉菜单的ng-repat中获取一些特定字段,现在,它是固定的cityName,我如何更改变量的cityName,该变量留在控制器上吗?

I build a dropdown directive, and it work with a different objects types, each object has own particular atributes, i need to get some particular field inside a ng-repat of my dropdown, now, it's fixed cityName, how i can change cityName for a variable, which stay on controller?

  <div class="listCombo" ng-show="showDrop" ng-mouseleave="lostFocus()">
        <table>
            <tr ng-repeat="result in results" ng-click="selectItem(result)">
                <td> {{result.cityName}} </td>
            </tr>
        </table>
    </div>

例如,我需要获取 peopleName ,而不是 cityName .

For example, i need to get peopleName rather than cityName.

推荐答案

是的,可以在 ng-repeat 中动态更改属性属性.下面是有关如何实现此目的的示例代码示例

Yes it is possible to change the property attribute dynamically in ng-repeat.Below is a sample example code on how to achieve this.

1)您应该具有以下数据源,以使事情变得简单

1) You should have data source like below to make things easy

$scope.objectsList = [{
                'name': 'Vikram',
                'Age': '25',
                'sex': 'M'
            }, {
                'name': 'Quantum',
                'Age': '26',
                'sex': 'F'
            }, {
                'name': 'Katty',
                'Age': '22',
                'sex': 'F'
            }];
            $scope.objName = 'name';

2)在您的HTML中, ng-repeat

2) In your HTML have something like this in your ng-repeat

<p ng-repeat="obj in objectsList">
    {{obj[objName]}} <!-- here by changing the 'objName' we can decide which property value to be displayed dynamically-->

</p>
<input type="text" ng-model="objName"/><!-- This is for example..u dont need this-->

如果您查看JS,我们已经提到 $ scope.objName ='name'; ,即,如果我们更改 $ scope.objName <,它将在列表中显示所有名称./code>设置为年龄",它将在数据源中显示相应的年龄.

If you look at JS we have mentioned $scope.objName = 'name'; i.e it will display all the names in a list,if we change the $scope.objName to 'Age' then it will display corresponding ages in the data source.

希望这能回答您的问题.

Hope this is answers your question.

这篇关于角度ng-repeat内部的动态属性引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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