-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Tabs] md-tab-link: routerLinkActive.isActive error #1967
Comments
Pls add a plunkr with repro. |
Please use the issue template which helps us reproduce and diagnose the issue. Including a plunkr with this bug will help us determine what is happening. |
This is exactly how I am trying to use this feature as well, super easy way to keep your tabs in line with your location. I am working on a plunkr but I am running into some issues just getting it set up.
I am assuming this is because it is a pretty new feature. I looked through systemjs.config and it seems to be asking for the latest, so I don't know where to go from here.
If someone can help me resolve these two issues I can finish up the plunkr and we can get this feature working. |
Thanks for the fast update. I updated the plunkr. All the relevant code is in |
I just tried adding the safe navigation operator to the |
I'm getting the same error when trying to use the value in to set a class: |
If you just need to add a class to the active link and remove it when it's not active, Angular has a built in directive that does that.
So, yours would be:
Here are some docs that might help. Here is a plunkr where You will notice that it updates the classes even if the redirect comes from a link outside of the |
Yes, I use that else where. The problem I am trying to solve is that I want to apply a class to something that is a child or my routerlink. Like this: |
If you can update that plunkr to demonstrate what you are looking for, I think I should be able to help you. |
Here is a fork of that plunkr. My goal is to be able to apply a class to something within the router link rather than the routerlink itself. I think it is the same issue as the OP because they are both trying to access the isActive within an expression. |
Okay... I've done everything I can think of. It seems these particular attributes , You can however use class bindings even in the child of the element containing
I would argue that this is actually cleaner than using Can anyone help me get the same functionality out of |
@DzmitryShylovich will your pull request #13341 address these issues? |
@nayfin yes, but it's not clear when it will be merged |
@DzmitryShylovich AFAICS your PR doesn't introduce backwards incompatible changes at the moment, please correct me if I'm wrong. @andrewseguin considering @DzmitryShylovich's PR is safe to merge, could you raise the priority of this issue. I believe this is pretty much how tabs component will be used in the most of the apps. /cc @jelbourn P.S. Sorry for bothering during the holidays 😉. Happy Christmas! 🎄 🎅 |
Good afternoon! I just ran into this very same issue today. When debugging my own code stil left me blocked, I turned to the web and found this thread. It changed the way I was looking at the issue and that helped me find a way to work around it in the immediate term. I haven't enough time to draft an example at the moment. I will follow up with one later on if this description is unclear, but want to offer what I have now in the meantime. It looks like the problem here is that the RouterLinkActive.isActive() method is dependent upon a @ViewChildren query-induced injection to occur. AFAIK, that happens during the content initialization stage of compilation. The property annotated with @ViewChildren is "this.links". The call to RouterLinkActive.isActive( ) is part of the same content model where RouterLinkActive is getting links injected from RouterLink. which explains why "this.links.changes.some( )" causes "Cannot read property 'some' of undefined" to be thrown--it isn't injected by the time the first call to isActive() expects it to be there, and the implementation of isActive( ) doesn't have a defensive check allowing it to return false when it's called so early. I confirmed that adding that undefined check in isActive( ) solved my issue, but modifying the framework isn't really a viable workaround. I couldn't make the framework tolerate an early call to isActive(), so I forced my component to defer its first call. Before workaround: After workaround: And in the Component code
The additional boolean stops the flow control from reaching isActive() until after the view initialization lifecycle event has passed, by which time the response to RouterLinkActive's @ViewChildren query has been injected and the call to isActive( ) therefore no longer gets tripped up on an uninitialized reference. Its not transparent, so its a work-around and not a solution, but it unblocked the rest of what I have yet to do early this week, so I'm comfortable with it for now. |
@jheinnic thanks. That's very helpful as I ran into this today as well! I had to make a small change to this to keep zone.js happy.
|
@andrewseguin This looks like it's a bug in Angular, specifically private hasActiveLink(): boolean {
return this.links.some(this.isLinkActive(this.router)) ||
this.linksWithHrefs.some(this.isLinkActive(this.router));
} Should guard against |
Just a note: each Is there maybe a way to reference specifically the element variable (e.g. app.component.html
app.component.ts
|
Thanks for the help and simple workaround until merge. Here is a plunkr of workaround for anyone who would like a full example. |
Hi everyone, I found this error after a sleepless night of thinking I was doing something wrong while trying to implement the documented MdTabNavBar Directive example shown at the bottom of https://material.angular.io/components/component/tabs#tabs-and-navigation (under "Tabs and Navigation").. Obviously, since this is still an open issue, that example (written verbatim) wont work.
|
After racking my head quite a while, I came to the same conclusion as @jelbourn. I think it's essential that in Has anyone raised this as an issue in the https://github.com/angular/angular repo yet? Or perhaps created a quick PR? |
I would additionally like to suggest the change below to the (Refer to: tab-nav-bar.ts) /* ... */
// New MdTabLink
@Directive({
selector: '[md-tab-link], [mat-tab-link]',
})
export class MdTabLink implements AfterContentInit {
private _isActive: boolean = false;
/** Whether the link is active. */
@Input()
get active(): boolean {
return this._isActive;
}
set active(value: boolean) {
this._isActive = value;
this._setActiveNavLink();
}
//flag to help double check that nav update only occurs when content is ready
private _contentReady: boolean = false;
ngAfterContentInit(){
this._contentReady = true;
this._setActiveNavLink();
}
private _setActiveNavLink(){
if (this.active && this._contentReady) {
this._mdTabNavBar.updateActiveLink(this._element.nativeElement);
}
}
constructor(private _mdTabNavBar: MdTabNavBar, private _element: ElementRef) {}
} |
add info to docs to avoid more people hitting this bug there have been quite a few reports about this recently it is not obvious to users that it is a core bug and not a bug in their code Relates to angular#1967
Should we mention in the docs this Just say the words and I can update the README.md to point this issue and th |
@vinagreti It does not look like they want to update any of the docs to address this bug. Instead they are trying to get the bug resolved ASAP. My PR to update the docs was declined. It looks like the PR in the core is waiting on review still. |
Angular 2.4.4 fixes the error and tabs change successfully; however [active] is not being set. |
Setting version to Angular 2.4.5 fixed it for me! |
Ok I got [active] set correctly this way:
Thanks to all who helped fix this. |
This still seems to be an open issue on Angular 4 with Angular Material 2.0.0-beta5. I could get neither the example on material io nor the fix proposed in this thread to resolve the issue. The browser console says |
angular 4.4.0-RC0 and material 2.0.0-beta.10 and this issue still occurs. none of the work-arounds work. any ideas ? is there another way to add components to each selected tab besides ? what do I do ? |
It's still an issue. Im using the following routes:
Then this as my component html:
So now it works on the initial load, but whenever I go back to the './' route the md-tab-link active doesn't get updated. UPDATE: I've fixed it by add this |
Seems like recent deprecation of |
Same issue with latest Angular 5.1.2 and Angular Material 5.02. My hacky/flaky workaround: ngAfterViewChecked() {
setTimeout(() => {
this.changeDetectionRef.detectChanges();
});
} |
@aretheregods Same issue with latest Angular 5.2.1 and Angular Material 5.1.0; Edit: |
This should be long ago fixed in Angular. |
and i'm still having it |
I'm having the same problem here :( |
@jelbourn This is still an open issue for me on the latest version. Please consider reopening this. |
I have the same problem here 👎 |
Having a similar issue, sadly I dont have the time to create a repro. |
If you are affected, please re-open if you can provide a reproduction of the issue, otherwise we won't be able to investigate. |
Having the same error. |
In my case the error was fixed by importing |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Trying to use the new md-tab-nav-bar like this:
but get an error
Seems like this way should work after commit: angular/angular@c9f58cf
The text was updated successfully, but these errors were encountered: