var myModule = angular.module(myModule', []);
myModule.directive('myDirective', function () {
return {
templateUrl: 'partials/timeline.html',
link: function postLink(elem, attrs, transclude) {
// This code will run after
// templateUrl has been loaded and cloned
}
};
);
The problem is divided in two steps, that must be handled each: The function "
link
" is called after the template has been cloned. This does not include any DOM manipulations that happen after this cloning, triggered by directives ng-repeat
or ng-view
. If you want to start your DOM manipulation after these directives have been handled by AngularJS (i.e. the DOM has been manipulated), you will need a setTimeout
, or better the $timeout
function from AngularJS.So now you will get this code.
var myModule = angular.module(myModule', []);
myModule.directive('myDirective', function ($timeout) {
return {
templateUrl: 'partials/timeline.html',
link: function postLink(elem, attrs, transclude) {
$timeout(function () {
// This code will run after
// templateUrl has been loaded, cloned
// and transformed by directives.
}, 0);
}
};
);
Yeah, that's better. But wait, we're not done yet. What if you want you want to do transformations of the DOM based on the positions of the newly rendered elements? Getting the position, offset, height, width of the newly created elements are not guaranteed to provide the correct values, because the browser had not enough time to render and layout those elements. The hack is to further delay these operations.
var myModule = angular.module(myModule', []);
myModule.directive('myDirective', function ($timeout) {
return {
templateUrl: 'partials/timeline.html',
link: function postLink(elem, attrs, transclude) {
$timeout(function () {
$timeout(function () {
// This code will run after
// templateUrl has been loaded, cloned
// and transformed by directives.
// and properly rendered by the browser
}, 0);
}, 0);
}
};
);
If you wonder, why a timeout of 0 really helps, here's a great explanation by John Resig.
I had a similar problem. I needed to search in element's inner DOM for certain selector.
ReplyDeleteWhen I used $(element) it returned empty array.
When I tried your solution with nested $timeout it worked, but I did not like it.
Finally I tried angular.element(elem).find(selector) and this worked. I did not have to use $timeout.
Thank you for your hint!
DeleteHi David,
ReplyDeleteI have a similar problem. Can you please give me an example to use this.
Thanks in advance.
What should be the right solution in your opinion?
ReplyDeleteUgly or not, it worked like a charm and I'll leave like so.
ReplyDeleteAfter hours trying to find a nice solution here.
I had to use it combined with a third-party core that I didn't want to refactor.
Thanks a lot for sharing!
Great solution. When nothing solved my issue of default focus to an element, this code worked as a charm.
ReplyDeleteInstead of two timeouts, would it be better if we put a single timeout with some delays greater than 0?
ReplyDeleteIt actually would work if the timeout is long enough.
DeleteThe problem is indeed, finding the right value so that this would always work, even on the slowest mobile device, but also work really fast on some high-end desktop computer. So I decided not to slowing down desktop computer users and risking of my code not to work correctly and went with the double $timeout.
Thanks a lot .
ReplyDeleteI have same type of issue
and i searched a lot on web but i din get any usefull link
later i found your link and i apply as you mention then it works nice.
Good Job dear
thank you so much...
This comment has been removed by the author.
ReplyDeleteI have some CSS transitions running during rerndering...the double timeouts do not seem to take care of this fact, and the code is run before/during the transitions
ReplyDeleteAre your transitions defined to take longer than 0 s? If they ware, you should consider taking a timeout delay of a value greater than 0 for the innermost timeout.
DeleteI think the content covered in the blog is quite impressive and brilliantly conveyed. Good job and great efforts. Keep it up.
ReplyDeleteAngularjs developer
Hi. Nice blog. It helped me very much. Thank you for sharing the information. Keep sharing.
ReplyDeleteangular js Online Training, angular js course, angular js online training in kurnool, angular js online training in hyderabad, angular js online training in bangalore, online courses, online learning, online education, trending courses, best career courses
It is nice blog Thank you provide important information and i am searching for same information to save my time AngularJS5 Online Training
ReplyDeleteYour article was too good I really appreciate it. I am working for BR Softech it is one of the best angularjs development company. But according to time, there is a lot of changes coming so I request to you please update it regularly & Keep it up.
ReplyDeleteColour Moon Technologies has the best and suitable solution for you. As we know Lunch Box Delivery is a very helpful to every Employees who was carving for the Neat, Hygienic and Homely food which is no where.
ReplyDeleteSo, Now you can start your own Online business by Design a website & Mobile App and Show case your lunch box items you offer, and the prices for each. Make it easy way to customers to place their orders online as well as to let you know whether they can give an dietary restrictions.
So what are you waiting for, Let's start up the Business plan and execute in a smart way by promoting through online. Join your hands with Colour Moon Technologies, the Experts in Developing Websites, Mobiles Apps and Gaming Apps with 10+ years of Experience.
https://bit.ly/2XY6rXw
Dengan begitu anda sebagai pemain juga harus pandai memaksimalkan waktu yang ada dengan sebaik mungkin
ReplyDeleteasikqq
dewaqq
sumoqq
interqq
pionpoker
bandar ceme terpercaya
hobiqq
paito warna terlengkap
bocoran sgp
I am impressed with your article , i am looking for angular js article but i also know a game development company who also write same article.
ReplyDeletethanks for the information... I have learned a lot from this blog.
ReplyDeleteAndroid game development company
ExpressTech Softwares – Top Angularjs Development company in India, offers best AngularJS development services for mobile / website application at affordable price. +91-9806724185 or Contact@expresstechsoftwares.com
ReplyDeleteWanted to take this opportunity to let you know that I read your blog posts on a regular basis. Your writing style is impressive, keep it up! Web Development Company in Bangalore | Web Design Company in Bangalore | Website Design Companies in Bangalore | Website Designing Company in Bangalore | Web Designing Company in Bangalore
ReplyDeleteThis comment has been removed by the author.
ReplyDeletenice blogbombay basera
ReplyDeleteHiiii...Thanks for sharing Great info...Nice post...Keep move on...
ReplyDeleteAngular JS Training in Hyderabad
Good Post. I like your blog. Thanks for Sharing
ReplyDeleteAngularJS Training Institute in Noida
This comment has been removed by the author.
ReplyDeleteSuch a useful and great information for me about handling DOM updates in AngularJS.
ReplyDeleteTo Hire Angularjs Developers visit Mobiwebtech.com
Turning out to be among one of the most favorable places to outsource projects, India is leading its way. There is no doubt that Indian app development companies are rated in the top development companies. If you also want to hire Indian app developers, you can rely on the list of top indian app development companies 2020.
ReplyDelete1. OpenXcell
2. The NineHertz
3. Indian App Developer
Dream11 is one of the most popular and trending fantasy app in the Indian subcontinent. It has emerged to become one of the biggest names in the fantasy sports genre of mobile applications. This application is popular because it allows the participants to win some quick money. If the participants have the right knowledge and skills, they can win a significant amount of money from each game they play. Are you wondering that how to make app like Dream11? However, you must understand that it is not an easy task to start a fantasy app development project.
ReplyDeleteNice information shared. Very helpful for developers to get knowledge from here. Thanks for share. Sales CRM Software
ReplyDeleteThanks for sharing this informative post with us. CRM
ReplyDeleteThe Best CRM that helps your business to grow Smarter, Better and Faster.
Thanks so much for sharing your wisdom and experience.sales outsourse
ReplyDeleteIt’s Great site sir,I am regularly following your site thanks for sharing with us.IT services
ReplyDeleteNice post..
ReplyDeleteSAP mm training
SAP pm training
SAP PP training
SAP Qm training
SAP Sd training
SAP Security training
SAP wm training
Scala training
Ah,so beautiful and wonderful post!An opportunity to read a fantastic and imaginary blogs.It gives me lots of pleasure and interest.Thanks for sharing.
ReplyDeleteData Science Training In Chennai
Data Science Course In Chennai
Our expert pharmacy app developers are able to create the best user friendly medicine delivery app that will make your customers lives easier and take your business to a new level.
ReplyDeletePharmacy App Development
Thanks for sharing this wonderful content.it's very useful to us. I gained a lot of information, the way you have clearly explained is really fantastic. Thanks a lot for this blog.
ReplyDeleteHire Dedicated Angularjs Developers
I am really happy to say it’s an amazing post to read. I learn new information from your article, you are doing a great job. Keep it up
ReplyDeleteHire AngularJS Developer
Pharma Coders provides readymade and customized Online Pharmacy App Development for different pharma business types.
ReplyDeleteperde modelleri
ReplyDeletemobil onay
Vodafone mobil ödeme bozdurma
nft nasıl alınır
ANKARA EVDEN EVE NAKLİYAT
TRAFİK SİGORTASI
dedektör
Site Kurmak
aşk kitapları
smm panel
ReplyDeletesmm panel
HTTPS://İSİLANLARİBLOG.COM
İNSTAGRAM TAKİPÇİ SATIN AL
hirdavatciburada.com
Https://www.beyazesyateknikservisi.com.tr/
servis
tiktok hile