使用 ng-repeat 或 ngFor 将 JSON 对象中的每个对象显示为单独的列表项

艾伦·马克·克里斯滕森

我收到一个看起来像这样的对象: 在此处输入图片说明

我正在尝试使用 ng-repeat 将每个对象的“消息”、“优先级”和“日期时间”属性显示为 ul 中的 li 项。

我尝试了几种方法,包括 ng-repeat 和 ngFor,其中所有方法都像第一个选项一样被包裹在 div 中:

  1. 这似乎是正确的方法,但完全不返回任何内容:

    <div style="background: red;"> <ul> <li ng-repeat="Notification in allNotifications">{{Notification}}</li> </ul> </div>

  2. 此选项按预期返回特定对象:

    <li style="border-radius: 3px" [ngStyle]="{'color' : alertText}" > Notification: {{ allNotifications.Notifications['0'].Message['0'] }} </li>

  3. 不编译:

    <li style="border-radius: 3px" [ngStyle]="{'color' : alertText}" [ngFor]="let subItem of allNotifications.Notifications['0'].Message['0']"> Notification: {{ subItem }} </li>

我的 TS 看起来像这样:

export class EventLogComponent implements OnInit {

  constructor(private _dashdata: DashdataService) { }

  NotificationName: string;
  alertText: string;
  allNotifications: JSON;

  ngOnInit() {
    this.NotificationName = 'NoNameAvailable';
    this.alertText = 'Black'; //TODO: set color according to threatlevel

    setInterval(() => {
      this._dashdata.getAllNotifications()
        .subscribe(res => {
          this.allNotifications = res['notificationdata'];
          console.log(this.allNotifications);
        });
    }, 5000); // Data Update interval in MS
  }
}
防火手套

使用 angular 你应该忘记 ng-repeat 是 AngularJS 的一部分(版本 <= 1.6)

我认为你有一个双重问题,第一个,如上所述,是 ng-repeat,第二个是你没有很好地定位你的数据。

试试这个

模板

<div style="background: red;">
  <ul>
    <li *ngFor="let not of allNotifications.Notification">{{not}}</li>
  </ul>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在 json 对象上使用 ng-repeat

将JSON对象转换为数组以使用Javascript通过ng-repeat进行迭代?

如何使用 forEach 从 ng-repeat 中删除 json 对象

将ng-repeat与Json Data结合使用

如何在Angular js中使用嵌套对象将ng-repeat中的数据作为JSON数据排序

如何使用ng-repeat将下面的json解析为如下所示的复杂表结构

使用ng-repeat将多个json的内容显示到单个div

将Json文件存储在AngularJS变量中以与'ng-repeat'一起使用

使用ng-repeat如何从嵌套的JSON对象检索数据

Angular2中的Ng-repeat-start-也使用NgFor重复多个元素

使用 ng-repeat 不显示列表对象

如何使用ng-repeat遍历JSON中的数组数组?

如何使用angularjs在ng-repeat中打印Json数据?

如何在Ionic框架中使用ng-repeat显示嵌套的JSON对象?

使用ngFor迭代Json对象

如何使用* ngFor显示json对象

如何替换使用ng-repeat显示的数组中的对象?

嵌套json对象的角度ng-repeat?

无法使用ng-repeat访问JSON

使用ng-if将额外的数据添加到ng-repeat结果中

ng-repeat显示json对象Angular JS

使用ng-repeat遍历对象数组

在JSON对象上使用ng-repeat不起作用-为什么?

仅在迭代JSON对象时才使用密钥的Neater ng-repeat语法

如何使用嵌套的ng-repeat循环两个JSON对象?

对Angular中的复杂json对象进行ng-repeat(迭代)

使用 ng-repeat 将鼠标悬停在表格中的文本上时显示图像

如何使用ng-repeat显示嵌套对象

如何使用 ng-repeat 显示数组/对象