Angular 无法推送到角度数组
我有一个过滤器从mongoDB获取所有数据,返回到日期在选定范围内的表。但是,当我运行代码时,控制台中会显示一条错误消息“无法读取未定义的属性”push“ app.component.html:Angular 无法推送到角度数组,angular,mean-stack,angular-filters,Angular,Mean Stack,Angular Filters,我有一个过滤器从mongoDB获取所有数据,返回到日期在选定范围内的表。但是,当我运行代码时,控制台中会显示一条错误消息“无法读取未定义的属性”push“ app.component.html: <div class="search"> <input type="text" matInput id = 'calander' ngxDaterangepickerMd [locale]="{ cancel
<div class="search">
<input type="text" matInput
id = 'calander'
ngxDaterangepickerMd
[locale]="{
cancelLabel: 'Cancel',
applyLabel: 'Okay',
clearLabel: 'Clear',
format: 'YYYY/MM/DD'
}"
startKey="start"
endKey="end"
[(ngModel)]="selected"
name="daterange"
(ngModelChange)="doSomething($event)"/>
<button class="ripple" type="submit" ></button>
</div>
<table>
<tr *ngFor="let email of filter(emails)" >
<td class="tg-0lax" id="tableText">{{ email.Sender}}</td>
<td class="tg-0lax" id="tableText">{{ email.Sent_To}}</td>
<td class="tg-0lax" id="tableText">{{ email.Subject}}</td>
<td class="tg-0lax"><!--{{ email.Attachment}} --></td>
<td class="tg-0lax"><b>{{ email.Created_On | date: 'yyyy/MM/dd'}}</b></td>
</tr>
</table>
export class AppComponent {
// Define a users property to hold our user data
emails: Array<any>;
filteredEmails: Array<any>;
startDate: any;
endDate: any;
doSomething(event){
this.startDate = new Date(event.start);
this.endDate = new Date(event.end);
}
filter(emails: any[]): any[] {
let filteredEmails = new Array()
if(this.startDate == null && this.endDate == null){
this.filteredEmails = this.emails;
return filteredEmails;
}
else{
this.filteredEmails.push(emails =>
emails.Created_On >= this.startDate && emails.Created_On <= this.endDate);
}
return filteredEmails;
}
constructor(private _dataService: DataService) {
// Access the Data Service's getUsers() method we defined
this._dataService.getEmails()
.subscribe(res => this.emails = res);
}
}
{{email.Sender}
{{email.Sent{}
{{email.Subject}
{{email.Created}日期:'yyyy/MM/dd'}
app.component.ts:
<div class="search">
<input type="text" matInput
id = 'calander'
ngxDaterangepickerMd
[locale]="{
cancelLabel: 'Cancel',
applyLabel: 'Okay',
clearLabel: 'Clear',
format: 'YYYY/MM/DD'
}"
startKey="start"
endKey="end"
[(ngModel)]="selected"
name="daterange"
(ngModelChange)="doSomething($event)"/>
<button class="ripple" type="submit" ></button>
</div>
<table>
<tr *ngFor="let email of filter(emails)" >
<td class="tg-0lax" id="tableText">{{ email.Sender}}</td>
<td class="tg-0lax" id="tableText">{{ email.Sent_To}}</td>
<td class="tg-0lax" id="tableText">{{ email.Subject}}</td>
<td class="tg-0lax"><!--{{ email.Attachment}} --></td>
<td class="tg-0lax"><b>{{ email.Created_On | date: 'yyyy/MM/dd'}}</b></td>
</tr>
</table>
export class AppComponent {
// Define a users property to hold our user data
emails: Array<any>;
filteredEmails: Array<any>;
startDate: any;
endDate: any;
doSomething(event){
this.startDate = new Date(event.start);
this.endDate = new Date(event.end);
}
filter(emails: any[]): any[] {
let filteredEmails = new Array()
if(this.startDate == null && this.endDate == null){
this.filteredEmails = this.emails;
return filteredEmails;
}
else{
this.filteredEmails.push(emails =>
emails.Created_On >= this.startDate && emails.Created_On <= this.endDate);
}
return filteredEmails;
}
constructor(private _dataService: DataService) {
// Access the Data Service's getUsers() method we defined
this._dataService.getEmails()
.subscribe(res => this.emails = res);
}
}
导出类AppComponent{
//定义用户属性以保存用户数据
电子邮件:数组;
过滤器demals:数组;
起始日期:任何;
结束日期:任何;
剂量测定(事件){
this.startDate=新日期(event.start);
this.endDate=新日期(event.end);
}
筛选(电子邮件:任意[]):任意[]{
让filteredEmails=newarray()
if(this.startDate==null&&this.endDate==null){
this.filteredEmails=this.emails;
回滤器水肿;
}
否则{
this.filteredEmails.push(电子邮件=>
emails.Created_On>=this.startDate&&emails.Created_On调用this.filteredEmails.push
的行试图在未初始化的数组上调用push
。您的组件有filteredEmails
属性,但它未初始化。我也不确定您为什么有本地filteredEmails
va变量以及具有相同名称的类属性
删除this
,或初始化属性filteredEmails:Array=[]
如果要使用组件类的属性,只需初始化该变量。如果只想使用方法中声明的filteredEmails
变量,请删除this
调用this.filteredEmails.push的行。filteredEmails.push
正在尝试对我调用的数组调用push
未初始化。您的组件有一个filteredEmails
属性,但它没有初始化。我也不知道为什么您有一个本地filteredEmails
变量以及一个同名的类属性
删除this
,或初始化属性filteredEmails:Array=[]
如果要使用component类的属性,只需初始化该变量。如果只想使用方法中声明的filteredEmails
变量,请删除此,如@Matt U所述,该属性未初始化;您只分配了类型
因此,通过分配一个空数组来初始化它
export class AppComponent {
emails = [];
filteredEmails = [];
...
此外,当使用空数组初始化时,不需要指定将自动完成的属性类型。
祝你好运,正如@Matt U所解释的,属性没有初始化;您只分配了类型
因此,通过分配一个空数组来初始化它
export class AppComponent {
emails = [];
filteredEmails = [];
...
此外,当使用空数组初始化时,不需要指定将自动完成的属性类型。
祝您好运我想您应该将数组初始化为过滤器
方法,如下所示:
filter(emails: any[]): any[] {
let filteredEmails = new Array<any>();
...
}
filter(电子邮件:any[]):any[]{
让filteredEmails=newarray();
...
}
我想您应该将数组初始化为过滤器
方法,如下所示:
filter(emails: any[]): any[] {
let filteredEmails = new Array<any>();
...
}
filter(电子邮件:any[]):any[]{
让filteredEmails=newarray();
...
}
像这样声明并清空数组:-
export class AppComponent {
emails: any[] = [];
filteredEmails: any[] = [];
}
emails.push(element);
然后按如下方式推送阵列中的元素:-
export class AppComponent {
emails: any[] = [];
filteredEmails: any[] = [];
}
emails.push(element);
希望这会有所帮助。像这样声明并清空数组:-
export class AppComponent {
emails: any[] = [];
filteredEmails: any[] = [];
}
emails.push(element);
然后按如下方式推送阵列中的元素:-
export class AppComponent {
emails: any[] = [];
filteredEmails: any[] = [];
}
emails.push(element);
希望这会有所帮助。你在电子邮件列表中得到了什么?你在电子邮件列表中得到了什么?谢谢你的帮助。我对angular很陌生,你的答案帮助了我。@DallanByrne请确保你接受你认为最能回答你问题的答案。谢谢你的帮助。我对angular很陌生,你的答案也是DallanByrne帮助了我。@DallanByrne请确保您接受您认为最能回答您问题的答案。