博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在AngularJS中使用ng-repeat迭代键和值?
阅读量:2289 次
发布时间:2019-05-09

本文共 2359 字,大约阅读时间需要 7 分钟。

本文翻译自:

In my controller, I have data like: $scope.object = data 在我的控制器中,我有以下数据: $scope.object = data

Now this data is the dictionary with keys and values from json . 现在这个数据是带有json键和值的字典。

I can access the attribute with object.name in the template. 我可以在模板中使用object.name访问该属性。 Is there any way that I can iterate over the keys as well and display them in table like 有没有什么方法可以迭代键,并在表格中显示它们

<tr><td> {

{key}} </td> <td> data.key </td>

The data is like this 数据是这样的

{    "id": 2,    "project": "wewe2012",    "date": "2013-02-26",    "description": "ewew",    "eet_no": "ewew",}

#1楼

参考:


#2楼

I don't think there's a builtin function in angular for doing this, but you can do this by creating a separate scope property containing all the header names, and you can fill this property automatically like this: 我不认为有角度的内置函数可以做到这一点,但你可以通过创建一个包含所有标题名称的单独范围属性来实现,你可以像这样自动填充这个属性:

var data = {  foo: 'a',  bar: 'b'};$scope.objectHeaders = [];for ( property in data ) {  $scope.objectHeaders.push(property); }// Output: [ 'foo', 'bar' ]

#3楼

How about: 怎么样:

{
{key}}
{
{ value }}

This method is listed in the docs: 此方法列在文档中: :


#4楼

If you would like to edit the property value with two way binding: 如果您想使用双向绑定编辑属性值:

    {
{key}}

#5楼

Here's a working example: 这是一个有效的例子:

{
{key}}
: {
{value}}

edited 编辑


#6楼

A todo list example which loops over object by ng-repeat : 一个todo列表示例,它通过ng-repeat循环遍历对象:

var app = angular.module('toDolistApp', []); app.controller('toDoListCntrl', function() { var self = this; self.toDoListItems = {};// []; //dont use square brackets if keys are string rather than numbers. self.doListCounter = 0; self.addToDoList = function() { var newToDoItem = {}; newToDoItem.title = self.toDoEntry; newToDoItem.completed = false; var keyIs = "key_" + self.doListCounter++; self.toDoListItems[keyIs] = newToDoItem; self.toDoEntry = ""; //after adding the item make the input box blank. }; }); app.filter('propsCounter', function() { return function(input) { return Object.keys(input).length; } });
  
Total Items: {
{toDoListCntrlAs.toDoListItems | propsCounter}}
Enter todo Item:
{
{toDoListCntrlAs.toDoEntry}}
{
{$index+1}} : {
{key}} : Title = {
{ prop.title}} : Status = {
{ prop.completed}}

转载地址:http://ijcnb.baihongyu.com/

你可能感兴趣的文章
iOS 运行发现,view随着屏幕的变大整体放大的问题
查看>>
IOS 使用itms-services协议,服务端安装应用
查看>>
关于 C++ 调用 python 详情
查看>>
IOS 使用NSURLProtocol 拦截网络请求实现缓存
查看>>
用Pyinstaller 打包 Python 生成 Mac OS X 应用
查看>>
使用python的mod_pbxproj 自动化 修改Xcode 工程
查看>>
Mac Security工具使用总结
查看>>
Python 多进程和多线程
查看>>
IOS keychain 使用
查看>>
IOS 指纹识别
查看>>
Mac OS 开发
查看>>
设计模式
查看>>
ArcGIS 关于三维立体地图 简单使用,里面的资源就在 arcgis 的demo里面有
查看>>
prime算法——最小生成树
查看>>
2020.01.05
查看>>
numpy,pandas学习
查看>>
github使用
查看>>
Python虚拟环境设置
查看>>
爬虫相关
查看>>
Python 报错中心
查看>>