无法将'ngIf'绑定到'div',因为它不是'div'的已知属性 - ionic/angular错误
无法将'ngIf'绑定到'div',因为它不是'div'的已知属性 - ionic/angular错误
最近几天我一直遇到这个错误。我已经阅读了其他帖子中关于相同错误的内容,并尝试了它们建议的方法,如在当前页面和app.module中添加CommonModule。我还尝试使用ng serve重置了服务器。
还有其他原因导致这个问题吗?我在应用程序的另一部分使用了几乎相同的代码,它可以正常工作。
edit-page.module.ts:
import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { IonicModule } from '@ionic/angular'; import { EditPagePageRoutingModule } from './edit-page-routing.module'; import { BrowserModule } from '@angular/platform-browser'; import { EditPagePage } from './edit-page.page'; import { SharedModule } from 'src/app/shared/shared.module'; import { AppComponent } from 'src/app/app.component'; @NgModule({ imports: [ CommonModule, ReactiveFormsModule, FormsModule, IonicModule, BrowserModule, EditPagePageRoutingModule ], declarations: [EditPagePage] }) export class EditPagePageModule {}
edit-page.page.ts:
@Component({ selector: 'app-edit-page', templateUrl: './edit-page.page.html', styleUrls: ['./edit-page.page.scss'], }) export class EditPagePage implements OnInit, OnDestroy { artist: Artist; artistId: string; arForm: FormGroup; isLoading = false; steps: any = 1; private artistSub: Subscription; constructor( private route: ActivatedRoute, private artistService: ArtistService, private navCtrl: NavController, private router: Router, private loadingCtrl: LoadingController, private alertCtrl: AlertController ) {} get genreControls() { return (this.arForm.get('genre')).controls; } get equipmentControls() { return ( this.arForm.get('equipment')).controls; } ngOnInit() { this.route.paramMap.subscribe(paramMap => { if (!paramMap.has('artistId')) { this.navCtrl.navigateBack('/tabs/tab4'); return; } this.artistId = paramMap.get('artistId'); this.isLoading = true; this.artistSub = this.artistService .getArtist(paramMap.get('artistId')) .subscribe( artist => { this.artist = artist; this.arForm = new FormGroup({ name: new FormControl(this.artist.name, { updateOn: 'blur', validators: [Validators.required] }), type: new FormControl(this.artist.type, { updateOn: 'blur', validators: [Validators.required] }), cost: new FormControl(this.artist.cost, { updateOn: 'blur', validators: [Validators.required] }), equipment: new FormArray([]), band: new FormControl(this.artist.band, { updateOn: 'blur', validators: [Validators.required] }), availableFrom: new FormControl(this.artist.availableFrom, { updateOn: 'blur', validators: [Validators.required] }), availableTo: new FormControl(this.artist.availableTo, { updateOn: 'blur', validators: [Validators.required] }), descrpition: new FormControl(this.artist.descrpition, { updateOn: 'blur', validators: [Validators.required, Validators.maxLength(180)] }), genre: new FormArray([]), }); this.isLoading = false; }, error => { this.alertCtrl .create({ header: '出错了!', message: '无法获取艺术家信息。请稍后再试。', buttons: [ { text: '好的', handler: () => { this.router.navigate(['/tabs/tab4']); } } ] }) .then(alertEl => { alertEl.present(); }); } ); }); } onAddGenre() { const control = new FormControl(this.artist.genre, [Validators.required]); ( this.arForm.get('genre')).push(control); } onAddEquipment() { const control = new FormControl(this.artist.equipment, [Validators.required]); ( this.arForm.get('equipment')).push(control); } addOne() { this.steps = this.steps + 1; console.log(this.steps); } minusOne() { this.steps = this.steps - 1; console.log(this.steps); } onUpdateArtist() { if (!this.arForm.valid) { return; } this.loadingCtrl .create({ message: '正在更新艺术家信息...' }) .then(loadingEl => { loadingEl.present(); this.artistService .updateArtist( this.artist.id, this.arForm.value.name, this.arForm.value.type, this.arForm.value.cost, this.arForm.value.equipment, this.arForm.value.band, this.arForm.value.availableFrom, this.arForm.value.availableTo, this.arForm.value.descrpition, this.arForm.value.genre, ) .subscribe(() => { loadingEl.dismiss(); this.arForm.reset(); this.router.navigate(['/tabs/tab4']); }); }); } ngOnDestroy() { if (this.artistSub) { this.artistSub.unsubscribe(); } } }
edit-page.page.html示例代码:
问题:Can't bind to 'ngIf' since it isn't a known property of 'div' - ionic/angular错误出现的原因和解决方法
在Ionic / Angular应用程序中,当尝试使用ngIf指令绑定到div元素时,可能会出现"Can't bind to 'ngIf' since it isn't a known property of 'div'"错误。这个错误的原因可能是由于以下两个原因之一:
1. 缺少BrowserModule:在edit-page.module.ts文件的imports中添加BrowserModule。确保在使用ngIf指令的页面或组件所在的模块中正确导入了BrowserModule。
解决方法:
import { BrowserModule } from '@angular/platform-browser'; @NgModule({ imports: [ BrowserModule ], }) export class EditPageModule { }
2. 组件未在app.module.ts中注册:检查app.module.ts文件,确认所使用的组件是否被正确注册。
解决方法:
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; import { YourComponent } from './your-component.component'; @NgModule({ declarations: [ AppComponent, YourComponent ], imports: [ BrowserModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
如果以上方法都尝试过仍然无效,可以尝试删除整个node_modules文件夹,并通过npm install进行全新安装。在执行这个操作之前,请确保停止服务器。
需要注意的是,如果在应用程序的其他部分中ngIf指令正常工作,可能不需要进行此操作。