Closed rlanga closed 4 years ago
Any specific reason you're injecting it on the constructor? How's your HTML file?
I used to be able to do one-way binding on an [open] property to vary when the progress bar opens like so:
<mdc-linear-progress [open]="showProgressBar"></mdc-linear-progress>
But I got errors in more recent versions of the mdc library about that not being possible. All I saw in the most recent linear progress bar documentation was two methods in the API: open() and close() but no example on how to use them. So I thought injecting into the constructor and initialising MdcLinearProgress with a reference to the html is how one calls these methods.
Is there another way to call those methods to change the open and close state?
I think the problem is that the bar is open by default and we usually don't want that.
I use the progress bar but I'm really a bit confused on how we should do it as there's really no real-case example in the documentation. Right now, I'm doing pretty much what you were trying to do, but instead of an [open] I just use an *ngIf, like so:
<mdc-linear-progress *ngIf="showProgressBar"></mdc-linear-progress>
If you want to use the API, you can try somethin like this:
HTML:
<mdc-linear-progress #progressbar></mdc-linear-progress>
TS:
import { Component, OnInit, ViewChild } from '@angular/core';
import {MdcLinearProgress} from '@angular-mdc/web/linear-progress';
@Component({
selector: 'app-progress-bar',
templateUrl: './progress-bar.component.html',
styleUrls: ['./prograss-bar.component.scss'],
})
export class ProgressBarComponent implements OnInit {
@ViewChild('progressBar') progressBar: MdcLinearProgress;
...
constructor() { } // No need to inject it here
ngOnInit() {
this.progressBar.close();
/* Example: Use it with a fetchservice
this.dataFetchService.getFetchStatus()
.subscribe((status) => {
status === 'fetching' ? this.progressBar.open() : this.progressBar.close()
}) */
}
}
@trimox I think we need a way to set the default state of the progress bar. Is there any current way for doing this? If not, may we get one?
Awesome! I can now access the linearProgress element. For some reason my styles aren't working at all so it's not visible but when I log the object to the console it shows up. I'm just having one issue after another.... Thank you for the code snippet above though!
You're welcome. About your styles, try setting this in your angular.json:
{
...
"projects": {
"your-project-name": {
...
"architect": {
...
"styles": ["src/styles.scss"], // May change the path, but it's your root stylestheet
"stylePreprocessorOptions": {
"includePaths": ["node_modules/"]
},
...
}
...
}
}
If that doesn't work, go the the "root stylesheet" (e.g "src/styles.scss") and add the following (be careful with global styles):
@use './app/components/progress-bar.component.scss';
Thanks for the tip! I realised I had to do the @use
style imports for each component I use. I've done global imports for some of them and it's working. Cheers for all the help @guilhermetod
Also @trimox maybe it might be worth adding a pointer in the documentation for the components to use ViewChild() referencing if people want to access the component methods? Idk, it might be an obvious thing to do... But I had also noticed that someone had a similar error #2123 . Please and thank you! :)
To Reproduce Module import as follows:
Component imported as follows:
Constructor is as follows:
Expected behavior
What Angular MDC version are you using? 6.0.0-canary.9
What OS are you using?: Ubuntu 16.04
What browser(s) is this bug affecting?: All
Additional context This issue is similar to #2123 in that I'm running Angular 9 but the error for me is being thrown for MdcLinearProgress. I've upgraded to 6.0.0-canary.9 as recommended in that post but I'm still getting the error. It was mentioned that Ivy is enabled by default and even when I added enableIvy: true to my tsconfig.json, nothing changes. I tried importing MdcLinearProgress in an empty stackblitz template and it gave me the same error in the Console.