首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NullInjectorError:没有用于ChangeDetectorRef的提供程序

NullInjectorError:没有用于ChangeDetectorRef的提供程序
EN

Stack Overflow用户
提问于 2018-02-22 05:59:00
回答 2查看 6.5K关注 0票数 5

使用Angular 5,我触发了一个函数,select()从仅用于触发另一功能的选择的一个组件,getqr()在用于打印的另一个组件中。getqr()激发对从第一个组件中选择的项的web请求。为了更新打印视图以反映请求,我使用ChangeDetectorRef。这样做会给我一个错误:

StaticInjectorError(AppModule)[PrintComponent -> ChangeDetectorRef]: StaticInjectorError(Platform: core)[PrintComponent -> ChangeDetectorRef]: NullInjectorError: No provider for ChangeDetectorRef!

我不确定这里的问题是什么,因为我不能(也不应该)添加ChangeDetectorRproviders在app.module.ts中。

以下是选择组件的TS:

代码语言:javascript
运行
复制
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { PrintComponent } from './print/print.component';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
  docs;

  constructor(public http: HttpClient, public print: PrintComponent) { }

  ngOnInit() {
    this.http.get("https://portal.oldnational.com/corporate/portalsupport/_api/web/lists/getbytitle('Document Separator Barcodes')/items?$top=1000&$orderBy=DocumentGroup,Title").subscribe(data => {
      this.docs = data['value'];
    });
  }

  select(ID, selected){
    if (selected == true) {
      this.print.selecteddocs.push(ID); //Adds selection to array on Print Component
    }
    else {
      var index = this.print.selecteddocs.indexOf(ID);
      this.print.selecteddocs.splice(index, 1); //Removes selection from array on Print Component
    }
    this.print.getqr(); //fires getqr() from Print Component
  }

}

以下是打印组件的TS:

代码语言:javascript
运行
复制
import { Component, OnInit, ChangeDetectorRef} from '@angular/core';
.....

@Component({
  selector: 'app-print',
  templateUrl: './print.component.html',
  styleUrls: ['./print.component.css'],
})
export class PrintComponent implements OnInit {
  barcodeitems;
  selecteddocs = [];

  constructor(public http: HttpClient, private cdr: ChangeDetectorRef){
  }

  ngOnInit(): void {
  }

  getqr() {
    console.log("selecteddocs array", this.selecteddocs);
    let filterQuery = this.selecteddocs.map(i => `ID eq '${i}'`).join(" or ");
    let url = `https://portal.oldnational.com/corporate/portalsupport/_api/web/lists/getbytitle('Document Separator Barcodes')/items?$top=1000&$orderBy=ID&$filter=${filterQuery}`
    this.http.get(url).subscribe(data => {
      console.log("data.value", data['value']); //Correctly logs results
      this.barcodeitems = data['value'];
      this.cdr.detectChanges(); //Attempt to refresh the view
    });
  }

}

做出选择后,如何解决此问题或更新打印组件的视图?

EN

回答 2

Stack Overflow用户

发布于 2021-02-25 22:45:12

在我的情况下,我打电话给changeDetectorRef: ChangeDetectorRef从一个服务。它工作得很好。一次changeDetectorRef: ChangeDetectorRef从服务移动到组件。

不将ChangeDetectorRef导入服务

票数 1
EN

Stack Overflow用户

发布于 2020-06-17 20:50:05

我已经通过将引用从app组件传递给ChangeDetector解决了这个问题,

例如,将构造函数更改为app.component.ts

代码语言:javascript
运行
复制
constructor(
    changeDetectorRef: ChangeDetectorRef,
    ms: MobileService) {
  ms.setChangeDetector(changeDetectorRef);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48916206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档