我正在尝试使用 ng-repeat 将每个对象的“消息”、“优先级”和“日期时间”属性显示为 ul 中的 li 项。
我尝试了几种方法,包括 ng-repeat 和 ngFor,其中所有方法都像第一个选项一样被包裹在 div 中:
这似乎是正确的方法,但完全不返回任何内容:
<div style="background: red;"> <ul> <li ng-repeat="Notification in allNotifications">{{Notification}}</li> </ul> </div>
此选项按预期返回特定对象:
<li style="border-radius: 3px" [ngStyle]="{'color' : alertText}" > Notification: {{ allNotifications.Notifications['0'].Message['0'] }} </li>
不编译:
<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] 删除。
我来说两句