-
Notifications
You must be signed in to change notification settings - Fork 3k
conditional parameter for ui-sref directive #1489
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
Comments
I was just looking for something like this. Was thinking it would be awesome if you could do
If the sref is currently active, display the arrow. If it's not, show the inactive thing |
@intellix I would prefer something like adding .ng-click-active (adding .ui-sref-active). But I think that's another use case than @bianchimro's. |
@intellix What about doing it with css and <a ui-sref="home" class="home" ui-sref-active="home_active">
<span class="arrow"></span>
<span class="opposite-arrow"></span>
</a> .home > .arrow {
display: none;
}
.home > .opposite-arrow {
display: block; /* or inline or whatever */
}
.home.home_active > .arrow {
display: block;
}
.home.home_active > .opposite-arrow {
display: none;
} |
Same goes for the top comment. |
@mallowigi I don't think so. I want the element itself to be the link and not its children and I don't want the link to be activated on a state basis but on a condition (angular expression) basis, |
First of all, what is a disabled link? Is it a link with no URL? I haven't come across such thing. Second of all, you can use interpolated ui-sref to return different states according to the expression. |
Sorry, probably i am using an incorrect term. But what I mean is just preventing the navigation to the linked url.
👍 Yes, that would make it, thanks for the suggestion
vs
Probably just a matter of taste. |
I think there is a ui-router plugin library out there for all kinds of additional behaviors, |
To be clear, WORKAROUND: |
A nice directive |
I find this better suited to the controller. <a ng-click="viewItem(item)"> Link to some state </a> $scope.viewItem = function(item) {
if (item.editing) {
alert('hold on there buddy.');
}
else {
$state.go('item', { id: item.id });
}
} |
Wow! @benbenwilde ! Your workaround made my morning!!!!!!!!!!!!!!!!! |
@markbrown4 using the controller does not add href to an |
Correct. My point was that I find conditional logic like this best left out of the template altogether. mallowigi has a good question "First of all, what is a disabled link?" |
If you're just trying to disable a click, you should be able to do it pretty simply by either intercepting the event and canceling it with One of the fundamental design ideas behind directives is that they're composable: using common interfaces (like bindings and DOM events) you can combine directives that don't know anything about one another in order to create new behavior. This means not overloading a single directive with many different responsibilities, and it also doesn't mean having to write lots of special cases for combining two pieces of otherwise-general behavior. Anyway, if my suggestion doesn't work (you shouldn't really need to write a scope method...), let me know and we'll address that. |
Disabling the click would be great except that it doesn't remove the href from the |
@benbenwilde why is that a problem? |
@markbrown4 I'm not even sure why your asking. Do you think it's ok if the user can't click the link yet they can still hover to see the link or right click to access the link? Anyways, a scenario this feature would be good for is when you use the same view for two different states, but only want a link in one of the states. For example, consider two states with the same view where in the first state a given link should work because 1: the user has permission, and 2: the ui-sref tag contains a valid relative state name (e.g. This would be a nice feature and would come in handy quite a bit, and cut down on duplicate html when u want dynamic links to different states. Especially useful when your The workaround is okay but is not super great because it only evaluates once on load. |
"Do you think it's ok if the user can't click the link yet they can still hover to see the link or right click to access the link?" I'm not convinced there should be a link there at all in your permissions use case, but you're right that stripping the href based on a condition might be an easier thing to implement in the view than changing the html. The real question is if ui-sref should be used at all if you don't want to generate a link. |
I do want to generate a link. @nateabele - you have a feature request and a use case in my previous comment. Thanks for your time! |
@nateabele - Possible implementation would be to add
Alternatively the expression could still be put on the
One more alternative would be to split up the state and condition, assuming we don't have a use case for changing the link to point to multiple different states, but merely turning it on or off. Then
Food for thought... Update - looking back at the first post in this issue, the original idea from @bianchimro is the 3rd solution above, but with |
@benbenwilde As I've tried to explain here and elsewhere, what you're suggesting is bad design. That kind of thinking results in a combinatorial explosion of unnecessary, case-specific directives. There are two things I'm willing to do that would give UI-Router the flexibility to accommodate the different use cases presented without devolving the design into a dozen case-specific bolt-ons:
|
Well, I don't think it is bad design, for example I have a case in which I use the same template to show a list of stores an to show the 'no-store' message, when there are no store results for your search or near you. |
@angelarted Please don't apologize for your English. Not only is it perfectly understandable, but I guarantee it is better than my Italian. 😉
You're right. Let me try to explain my point a little differently. The idea is to develop small directives that can be composed together easily for each specific situation. In this case, a small, general-purpose directive with a high Plus, then you don't have to have to figure out general-purpose answers for the following:
The problem with these requests is that they all involve very general things. However, the way we choose to combine those things is unique to each developer's application. Your use case is probably not as general as you think, and supporting everyone's non-general use cases is a very quick path to a very bloated codebase. The purpose of UI-Router is to provide a set of tools within the context of the Angular platform to define an application as a state machine, and give developers general tools to manage it as such. Making a |
Seems like the discussion has somewhat diverged from the initial issue, but this is very interesting anyway. My two cents:
So, IMHO, this is not a problem of having many directives that can be combined in too many ways or can generate confusion. I just find this
more elegant than this
So I'd like to have this shortcut, but this is just a matter of taste. |
Totally agreed, it should just be a separate directive. When I say 'design', I'm talking software architecture, not UI.
Again, though, whether it's actually a separate directive or not is beside the point. We could invent a thousand of these attributes for every single use case imaginable, and I would never get another release out, because the (already massive) issue queue would be multiple orders of magnitude bigger than it already is. 😛
Agreed, and This is pretty elegant, too: <span ui-sref="app.somestate" my-disabled="!editing">Link to some state</span> All the more elegant because a custom directive (i.e. |
I'd suggest closing this. The |
Closing this issue since a couple of options have been proposed in the discussion. Thanks everyone. |
This is exactly the kind of thing I'm talking about. |
without using router can we the link the pages .....another way for linking pages???? |
+1 @benbenwilde for the work around of using example: {{item.editable ? 'app.groupsdetail({id: item.$id})' : '.'}} |
|
The dot is throwing an error. invalid state. |
@benbenwilde Is this work around working Just commenting on your previous comment can i write some random state instead of .childState like for example {{condition?'welcome':'.'}}
|
Duplicate issue #2957 (fixed) 'ng-disabled' is no longer impotent on anchor tags. Example: HTH |
For those looking for a working, out-of-the-box solution to dynamic ui-srefs:
read more: https://ui-router.github.io/ng1/docs/1.0.14/modules/directives.html#uistatedirective |
It would be nice to have an additional parameter to the
ui-sref
directive to add a condition to the activation of the link, just likeng-disabled
acts for a button or an anchor.For example a
ui-sref-if
param, if present, could enable the link when a condition is true:(desired behaviour: link is not active if
editing
istrue
)Another possible approach could be a parameter that disables the link when a condition is true like
ui-sref-disabled
:(same desired behaviour: link is not active if
editing
istrue
)The text was updated successfully, but these errors were encountered: