-
Notifications
You must be signed in to change notification settings - Fork 27.4k
support resolve option for directives #2095
Comments
We want directives to be as synchronous or as close to synchronous as possible so that we reduce user perceived flicker and jank. Delaying directive resolution would work against this goal. |
I'm closing this, please feel free to reopen if you have suggestions on how to avoid the flicker issue. |
The only solutions I can come up with would immediately lead to very poor performance due to the delays in rendering (e.g., block parent directive rendering on child directive resolution). Even so, I think there are certain times where "flicker" is acceptable (e.g. late-loading comments below a blog) and would like the freedom to code my controller in a synchronous fashion even though the data is fetched asynchronously. |
This is my BIGGEST gripe with Angular. I think there should be a concept of a loading state in angular given 99% of projects are likely to need a loading state of sorts. Let people define their own templateUrl for the directive while it's in a loading state. I'm constantly having to deal with directives that can't cope with asynchronously loading data. Even angularStrap can't deal with bs-typeahead="obj.someArray" being asynchronously loaded. e.g.
It would be wise to assume that asynchronous developers are going to want to put loading gifs in their apps. Why not make it easy? Without being natively , more Angular apps are going to look janky |
In my humble opinion, there definitely needs to be something done to simplify handling asynchronous data and directives (it is driving me insane too) - everyone keeps on saying how DOM manipulation should be done in directives, but often DOM manipulation in directives depends on asynchronous data having been loaded first, so given this I don't understand how/why directives are designed to be 'as close to synchronous as possible'? I am relatively new to Angular, and maybe I just need to be pointed to a good article on directives with asynchronous data, but 'setTimeout' just doesn't cut it, and from what I understand there is "no way to know for sure when a directive has finished loading" (eg. can't use viewContentLoaded), so I'm unclear how it should be handled (and it doesn't make sense to me currently either). Whether there is a "loading state" or perhaps better still a way of knowing when a directive has finished loading, I don't know, but I'd love to know how this should be handled the 'Angular way'. |
Matt, I agree, it is counter-intuitive. They way we currently deal with the async loading is we always have to wrap It would be a simple win-win fix just to add a 'resolve' state to Arush Sehgal twitter: founders.getbrandid.comBRANDiD is a Seedcamp http://www.seedcamp.com/ companyFollow us on AngelList: angel.co/brandid The information contained in this email is intended for the named recipient On Thu, Apr 25, 2013 at 2:35 AM, Matt Jensen [email protected]:
|
it would be a great feature to have! |
I agree it would be a good idea to facilitate a loading state + resolve for directives |
👍 |
+1 Yeap, just dealing with a lot of async data and promises, resolve in directive will defiantly enhance user usability |
+1 |
+1 here |
+1. @IgorMinar, the original reason for closing doesn't seem like a complete explanation. Lots of things can cause jankiness and flickering, |
+1 for |
I have a implemented a lazy-loading mechanism which hijacks the route resolver to crawl a template looking for directives, making sure those directives and their dependencies are loaded, then does so recursively in the directives' templates and so on. Only once all the dependencies are loaded does the route resolve, so there's no flicker. Angular could do something similar out of the box? It sounds like async support is on the table for 2.0 so I'm guess we'll see some kind of solution then... |
+1 I am looking for an easy way to have local loading states, and having a resolve function on a directive would solve the problem. |
I'm pretty new in Angular but I can't understand at all: suppose I use a directive to display information I got via And what's the difference between controllers and directives here? @ewinslow where can we see your code? |
+1 |
@kuraga https://github.com/ewinslow/ng-require is the lazy-loading library. AMD-based. https://github.com/ewinslow/ng-elgg is an example library that depends on it. |
+1 |
1 similar comment
👍 |
While I think directives should not rely on resolve() by default, it should be an option. |
+1 |
Looks like everyone needs this feature but no one suggests a solution on how to workaround the lack of resolve. Please can somebody provide an example? I just don't get how to populate directive with data from $http. And of course +1 for the feature. |
@OnkelTem, There are a couple ways:
You probably should not inject $http into the directive, but here's how to do it: app.directive('foo', ['$http', function FooDirective($http) {
$http.get('…').success(fn(){});
return {
link: function postLink(scope, element, attrs) { }
};
}]); |
Still looking for a clean solution for this one. Currently I manually delay the initialization of my directives until all dependencies are resolved. That results in really smelly code .. |
You could put together a PR for this. I can't promise it would land, but I have some ideas how you could do it. It would be something like this:
As Igor has said, this is probably not desirable and not necessarily likely to land, but if you can implement it elegantly and demonstrate that it's actually useful, there's a chance it could land since there seems to be considerable interest. |
+1 |
1 similar comment
+1 |
Is it too late for me to add a +1 ? |
+1 |
+2 critical for cross-platform spas |
this seems to be a solution for this issue : http://www.yearofmoo.com/2012/10/more-angularjs-magic-to-supercharge-your-webapp.html |
+1 |
8 similar comments
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
I made a gist containing a quick edit of my approach to support resolves in directives: https://gist.github.com/mntnoe/56ad60b11549a877d4cc Hope it may give some inspiration on how resolves could indirectly be supported by using a wrapper function around the directive definition object. |
+1 |
4 similar comments
+1 |
+1 |
+1 |
+1 |
+1 Why not have it as an option, right? |
+1 |
+1 |
2 similar comments
+1 |
+1 |
+1 @AlexCppns I had the same question when I browsed to this topi; but seems it is not. |
+1 |
1 similar comment
+1 |
There are several ways to achieve this that others have mentioned in this thread. As @caitp noted, there's not a great way to implement this without greatly increasing the already complex Angular 1 compiler. The good news is that Angular 2 supports this (and much more) at the DI layer in a way that's cohesive and doesn't introduce a ton of additional complexity. I'm going to lock this thread, but thank you all for your interest and feedback! |
They have
template
andcontroller
options. Seems consistent to give them aresolve
option as well, just like routes, unless there's some philosophical reason for leaving them out in this case...The text was updated successfully, but these errors were encountered: