Recently diving into Angular 4, I embarked on a mission to craft a dynamic component that showcases a loading indicator while crucial content is being fetched - think login credentials or data for a chart. Steering away from utilizing plugins, my main goal was to unravel the intricacies of crafting such a feature myself.
Using the CLI command ng g component loading, I kickstarted the creation process for the component. Subsequently, I hatched a service responsible for toggling the visibility of this component at will.
Within 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 delves into the visual rendition:
<div class ="box-loading" *ngIf="loading">
<div id="loading"></div>
</div>
The orchestration stems from 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();
}
}
Despite invoking the showLoading() method yielding no results, my troubleshooting led me to test
Error: Template parse errors:
'app-loading' is not a known element:...
In efforts to combat this dilemma, I appended CUSTOM_ELEMENTS_SCHEMA to both loading.module.ts and app.module.ts:
loading.module.ts:
import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { CommonModule } from '@common';
import { LoadingComponent } from './loading.component';
@NgModule({
declarations: [LoadingComponent],
exports: [LoadingComponent],
imports: [LoadingComponent],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class LoadingModule{}
Venturing forward, how can I seamlessly embed my component within the HTML framework? Does any standard practice stipulate the correct approach for achieving this integration successfully? Am I treading the right path towards my end goal?