使用Angular 5,我触发了一个函数,select()
从仅用于触发另一功能的选择的一个组件,getqr()
在用于打印的另一个组件中。getqr()
激发对从第一个组件中选择的项的web请求。为了更新打印视图以反映请求,我使用ChangeDetectorRef
。这样做会给我一个错误:
StaticInjectorError(AppModule)[PrintComponent -> ChangeDetectorRef]: StaticInjectorError(Platform: core)[PrintComponent -> ChangeDetectorRef]: NullInjectorError: No provider for ChangeDetectorRef!
我不确定这里的问题是什么,因为我不能(也不应该)添加ChangeDetectorR
至providers
在app.module.ts中。
以下是选择组件的TS:
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:
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
});
}
}
做出选择后,如何解决此问题或更新打印组件的视图?
发布于 2021-02-25 22:45:12
在我的情况下,我打电话给changeDetectorRef: ChangeDetectorRef
从一个服务。它工作得很好。一次changeDetectorRef: ChangeDetectorRef
从服务移动到组件。
不将ChangeDetectorRef导入服务
发布于 2020-06-17 20:50:05
我已经通过将引用从app组件传递给ChangeDetector解决了这个问题,
例如,将构造函数更改为app.component.ts
至
constructor(
changeDetectorRef: ChangeDetectorRef,
ms: MobileService) {
ms.setChangeDetector(changeDetectorRef);
}
https://stackoverflow.com/questions/48916206
复制相似问题