简体   繁体   English

为什么会出现TypeError:undefined不是Angular中的对象(求值”)?

[英]Why I get TypeError: undefined is not an object (evaluating '') in Angular?

I have custom service service.center.ts which contains the following delete method: 我有自定义服务service.center.ts ,其中包含以下删除方法:

deleteCenter(id: number): Observable<{}> {
  console.log(id);
  const url = `${this.D_ROOT_URL}${id}`;
  return this.http.delete(url);
}

I have the button in the template: 我在模板中有按钮:

<button type="submit" (click)="deleteCenter()">Delete</button>

which calls the following method in list-centers.component.ts : list-centers.component.ts调用以下方法:

deleteCenter(): void {
  if (this.getCheckedCenters.length > 0) {
    this.getCheckedCenters.forEach(function (id) {
      id = +id;
      this.centers = this.centers.filter(c => c.id !== id);
      this.centerService.deleteCenter(id).subscribe();
    });
  }
}

The this.getCheckedCenters returns the array of ids, and I printed with console.log which returns valid number. this.getCheckedCenters返回ID数组,我打印了console.log并返回有效数字。

But the method in list-centers.component.ts print error in console, and doesn't call another method in the center.service.ts . 但是list-centers.component.ts的方法在控制台中显示错误,并且不会在center.service.ts调用另一个方法。

Error: 错误:

TypeError: undefined is not an object (evaluating 'this.centers') TypeError:未定义不是对象(评估“ this.centers”)

centers: Center[];

Data for centers array: 中心数组的数据: 在此处输入图片说明

By using foreach(function..., you are losing the this scope. You can try using a lambda foreach(p => {... 通过使用foreach(function ...,您将失去此作用域。您可以尝试使用lambda foreach(p => {...

This is because of the way typescrypt guarantees scopes in lambdas but not function. 这是因为typecrypt保证了lambda中的作用域而不是功能。 You could alternatively use function and a closure 您也可以使用函数和闭包

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未定义不是对象(评估)-角度 - Undefined is not an object(evaluating) - angular TypeError: undefined is not an object(评估'ref.onSnapshot') - TypeError: undefined is not an object (evaluating 'ref.onSnapshot') TypeError:undefined不是对象(评估&#39;res [0] .id&#39;) - TypeError: undefined is not an object (evaluating 'res[0].id') TypeError:未定义不是对象(评估“ parentOutlet.peekState”) - TypeError: undefined is not an object (evaluating 'parentOutlet.peekState') undefined 不是对象(评估 &#39;this.http.get&#39;) - undefined is not an object (evaluating 'this.http.get') 例外:TypeError:未定义不是Ionic 2中的对象(评估“ pipe.constructor”)吗? - EXCEPTION: TypeError: undefined is not an object (evaluating 'pipe.constructor') in Ionic 2? undefined不是对象(正在评估“ config.forEach”)-Angular2路由测试 - undefined is not an object (evaluating 'config.forEach') - Angular2 Route Test IE9中的Angular 1/2混合应用程序错误:“ TypeError:无法获取属性&#39;ref&#39;的值:对象为null或未定义” - Angular 1/2 hybrid app error in ie9: “TypeError: Unable to get value of the property 'ref': object is null or undefined” 我不知道为什么我会收到以下消息:“ ERROR TypeError:无法设置未定义的属性&#39;position&#39;” - I don't know why I receive this message in angular: “ERROR TypeError: Cannot set property 'position' of undefined” TypeError:未定义不是构造函数(正在评估“ new viewClass()”) - TypeError: undefined is not a constructor (evaluating 'new viewClass()')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM