I'm new on Angular 4 and I'm trying to create a component that shows a loading box while some content is being loaded... like login, loading a chart, etc. I don't want to use plugins to do it because I want to learn how to do. I've created the component using the CLI command ng g component loading and then I created a service that calls a method to show or hide the component from view.
loading.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-loading',
templateUrl: './loading.component.html',
styleUrls: ['./loading.component.scss']
})
export class LoadingComponent implements OnInit {
private loading = false;
constructor() { }
ngOnInit() {
}
public showLoad(){
this.loading = true;
}
public hideLoad(){
this.loading = false;
}
}
loading.component.html
<div class ="box-loading" *ngIf="loading">
<div id="loading"></div>
</div>
loading.service.ts
import { Injectable } from '@angular/core';
import { LoadingComponent } from '../../loading/loading.component';
@Injectable()
export class LoadingService {
constructor(private loading: LoadingComponent) { }
public showLoading(){
this.loading.showLoad();
}
public hideLoading(){
this.loading.hideLoad();
}
}
When I invoke the showLoading() method, nothing happens. So I decided to test the <app-loading></app-loading> on my login page, but I'm getting the following error.
ERROR Error: Uncaught (in promise): Error: Template parse errors:
'app-loading' is not a known element:
1. If 'app-loading' is an Angular component, then verify that it is part of this module.
2. If 'app-loading' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
<div class="login-box card">
<div class="card-body">
[ERROR ->]<app-loading *ngIf="loading"></app-loading>
<form class="form-horizontal floating-labels" id="log"): ng:///LoginModule/LoginComponent.html@4:3
I added the CUSTOM_ELEMENTS_SCHEMA to the ngModule in the loading.module.ts and in the app.module.ts as well.
loading.module.ts
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LoadingComponent } from './loading.component';
@NgModule({
declarations: [LoadingComponent],
exports: [LoadingComponent],
imports: [LoadingComponent],
schemas: [ CUSTOM_ELEMENTS_SCHEMA]
})
export class LoadingModule{}
How can I inject my component into a html page? Is there a best practice to do it? Am I on the right way?
entryComponentsarray in yourapp.module.tsfile or your feature module. angular.io/guide/entry-components