Angular “更改探测器参考不使用角度材质”对话框
我需要帮助来正确实施ChangeDetectorRef 我试图做的是单击打开一个Mat对话框,但每次我添加ChangeDetectorRef构造函数时,对话框就会中断,并给出错误信息 NullInjectorError:StaticInjectorErrorAppModule[ViewAdvertComponent->ChangeDetectorRef]: StaticInjectorErrorPlatform:core[ViewAdvertComponent->ChangeDetectorRef]: NullInjectorError:没有ChangeDetectorRef的提供程序 我可能只是引用错了,但就我所知,即使是文档也太复杂了 HomePage.tsAngular “更改探测器参考不使用角度材质”对话框,angular,typescript,angular-material,Angular,Typescript,Angular Material,我需要帮助来正确实施ChangeDetectorRef 我试图做的是单击打开一个Mat对话框,但每次我添加ChangeDetectorRef构造函数时,对话框就会中断,并给出错误信息 NullInjectorError:StaticInjectorErrorAppModule[ViewAdvertComponent->ChangeDetectorRef]: StaticInjectorErrorPlatform:core[ViewAdvertComponent->ChangeDetectorRe
import { Component, OnInit, NgModule, ViewChild, ChangeDetectorRef } from '@angular/core';
import { MaterialModule } from 'src/materialModule';
import { LoginService, iMakes } from '../login-popup/login.service';
import { HomePageService } from './home-page.service';
import { environment } from 'src/environments/environment';
import { HttpClient } from '@angular/common/http';
import { MatSnackBar } from '@angular/material/snack-bar';
import { RegisteredUserService } from '../registered-user/registered-user.service';
import { MatDialog } from '@angular/material/dialog';
import { ViewAdvertComponent } from '../view-advert/view-advert.component';
import { SendAdvertIDService } from './sendAdvertID.service';
import { MatPaginator } from '@angular/material/paginator';
import { Observable } from 'rxjs';
import { MatTableDataSource } from '@angular/material/table';
@NgModule({imports:[MaterialModule]})
@Component({
selector: 'app-home-page',
templateUrl: './home-page.component.html',
styleUrls: ['./home-page.component.css'],
providers:[ViewAdvertComponent]
})
export class HomePageComponent implements OnInit {
@ViewChild(MatPaginator,{static:false}) paginator: MatPaginator;
public apiURL = environment.api;
public isImageLoading:any;
public adverts:any=[];
public imageToLoad:any=[];
public obs: Observable<any>;
public dataSource:MatTableDataSource<Card>;
public selected;
public advertID:any;
constructor(private cd: ChangeDetectorRef,private advertIDService:SendAdvertIDService, private popup:MatDialog,private http:HttpClient,private lservice:LoginService, private snack:MatSnackBar,private registeredUserService:RegisteredUserService) {}
ngOnInit() {
this.registeredUserService.GetAllAdverts().subscribe(val=>
{
this.dataSource = new MatTableDataSource<Card>(val);
this.dataSource.paginator = this.paginator;
this.obs = this.dataSource.connect();
let numOfAds=0;
for(let a of val)
{
this.imageToLoad[numOfAds]={url:this.apiURL+'/api/Images/'+a.advertID+'_1'};
numOfAds++;
}
},error => this.snack.open("Something went wrong. Please try again later")._dismissAfter(5000));
}
ViewAdvert(e) //method that opens the dialog box
{
this.cd.markForCheck();
this.advertIDService.SetAdvertID(e);
this.popup.open(ViewAdvertComponent,{
width: '500px',
});
}
在对话框内执行的viewatter.ts
import { Component, ViewChild, ChangeDetectorRef, ChangeDetectionStrategy} from '@angular/core';
import { HomePageComponent } from '../home-page/home-page.component';
import { SendAdvertIDService } from '../home-page/sendAdvertID.service';
import { ViewAdvertService, iAdvertID } from './view-advert.service';
import { environment } from 'src/environments/environment';
@Component({
selector: 'app-view-advert',
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './view-advert.component.html',
styleUrls: ['./view-advert.component.css']
})
export class ViewAdvertComponent {
@ViewChild('slideshow',{static:false}) slideshow:any;
public advertID:String;
public apiURL = environment.api;
public imageID:any;
public imageSource:any=[];
public imageToSlideShow:any=[];
public counter:any=0;
public faultyImages:any=[];
constructor(private cd: ChangeDetectorRef,public home:HomePageComponent,private advertIDService:SendAdvertIDService, private viewService:ViewAdvertService) {
this.advertID=home.advertID;
}
ngOnInit(e) {
this.cd.markForCheck();
this.advertID= this.advertIDService.GetAdvertID();
let param:iAdvertID={
id:this.advertID
}
this.viewService.AdvertDetails(param).subscribe(val=>
{
this.imageID=val[0].advertID;
this.counter=1;
for(let i=0;i<=4;i++)
{
this.imageSource[i]=this.apiURL+'/api/Images/'+this.imageID+'_'+this.counter;
this.faultyImages[i]=e;
if(this.imageSource[i]==this.faultyImages[i])
{
console.log(this.faultyImages[i]);
}
else{
this.imageToSlideShow[i]=this.imageSource[i];
}
this.counter++;
}
});
}
}
我怀疑您收到错误是因为您没有正确使用依赖项注入。当您想要将一个类注入到另一个类中时,您需要将@Injectable{}decorator添加到注入的类中。在英语中,这样的类通常被称为服务。请查看文档。我怀疑您收到了错误,因为您没有正确使用依赖项注入。当您想要将一个类注入到另一个类中时,您需要将@Injectable{}decorator添加到注入的类中。在英语中,这样的类通常被称为服务。请查看文档。您不能将组件ViewAdvertComponent注入另一个组件HomePageComponent
但是我认为您不需要这个,但是这个。您不能将组件ViewAdvertComponent注入到另一个组件HomePageComponent中
但是我想你不需要这个,但是这个。请确切地告诉我你在做什么?你在学习如何用角度编程?或者你尝试更新一些应用程序?这是我最初的目标,但我有点迟钝,只是一步一步地学习一些基础教程。或者甚至从这边开始读,试着解开这个:请确切地告诉我你在做什么?你在学习如何用角度编程?或者你尝试更新一些应用程序?这是我最初的目标,但我有点迟钝,只是一步一步地学习一些基础教程。或者甚至从这边开始读,试着解开这个: