[英]Angular 5 make local variable to global
I have a code like this : 我有这样的代码:
var data = [
{
first_name: "Apurv",
date: "2018-01-22",
is_present: "1",
},
{
first_name: "Lucky",
date: "2018-01-22",
is_present: "0",
},
{
first_name: "Apurv",
date: "2018-01-20",
is_present: "0",
},
{
first_name: "Lucky",
date: "2018-01-20",
is_present: "1",
}
];
var groupByName = {};
data.forEach(function (a) {
groupByName [a.first_name] = groupByName [a.first_name] || [];
groupByName [a.first_name].push({ date: a.date, is_present: a.is_present });
});
console.log(groupByName);
I try to make local variable of groupByName
variable become global in order to works with *ngIf on Angular 5, could everyone clearing this or maybe there is another way to solve this issue? 我试图使
groupByName
变量的groupByName
变量成为全局变量,以便与Angular 5上的* ngIf一起使用,是否所有人都可以清除此变量,或者还有其他方法可以解决此问题? I would be thank you before! 我将在此之前谢谢你!
You should do it as a property of your component : 您应该将其作为组件的属性:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-mycomponent',
template: '<some html here>',
})
export class MyComponent implements OnInit{
public data:any[];
public groupByName:any;
ngOnInit() {
this.data = [
{
first_name: "Apurv",
date: "2018-01-22",
is_present: "1",
},
{
first_name: "Lucky",
date: "2018-01-22",
is_present: "0",
},
{
first_name: "Apurv",
date: "2018-01-20",
is_present: "0",
},
{
first_name: "Lucky",
date: "2018-01-20",
is_present: "1",
}
];
this.groupByName = {};
this.data.forEach(function (a) {
this.groupByName[a.first_name] = this.groupByName [a.first_name] || [];
this.groupByName[a.first_name].push({ date: a.date, is_present: a.is_present });
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.