Execute Javascript Code Once Directive Has Finished Rendering the DOM for ShareThis

Within my Angular (1.3) application, I have a list of records being displayed using ng-repeat. The template includes a directive that contains ShareThis controls from ShareThis, which are activated once the DOM is loaded.

Upon the initial load of the app, the ShareThis Javascript functions correctly and activates the buttons. However, when there is a route change, the activation does not occur. I have come across references to manually activate the controls using stButtons.makeButtons() or stButtons.locateElements();. But I am uncertain about where to call this function in the directive or page cycle. I've tried placing it within:

  1. the directive link function - utilizing $timeout or scope.$watch
  2. the template
    <script>stButtons.locateElements();</script>
    - activates before model binding
  3. the controller after binding - activates before DOM rendering

I understand that the activation function needs to be called after binding and after DOM rendering, but Angular does not provide an indication for when the DOM is ready. There is a method to dynamically render the ShareThis controls using only Javascript, but I prefer having the HTML defined in the template rather than in Javascript for this particular case.

Despite encountering various related questions on this issue, none of the answers seem to fully resolve the problem in my scenario, especially considering the changes introduced in Angular 1.3.

item-list.html (view)

<div ng-repeat="item in vm.itemList">
    <item-directive item="item"></item-directive>
</div>

item-list.cs (controller)

{ ... vm.itemList = getItems(...) ... }

item-directive.js (directive)

(function () {
    angular.module('app');
    function itemDirective() {
        var directive = { templateUrl: 'item.html', link: linkFunc, controller: ItemDirective };
        return directive;
        function linkFunc(scope, element, attr, ctrl) { var item = scope.item }
    }
    ItemDirective.$inject = ['$scope'];
    function ItemDirective($scope) { ... }
}

item.html (directive template)

...
<div class="item-share-section">
    <span class='st_sharethis_large' st_url="{{vm.item.url}}" st_title="{{vm.item.name}}"></span>
</div>
...

Answer №1

It seems like you're looking to execute the function only after the DOM has finished rendering, am I correct? One way to achieve this is by inserting the following code inside the postLink method of your directive:

$scope.$watch('viewContentLoaded', stButtons.locateElements())

Answer №2

Although some may say my solution is a bit unconventional, I find it to be more effective than using $watch, which can be inefficient. Instead of relying on $watch, I choose to initialize the function responsible for loading buttons when the specific view requiring button loading is rendered. Here's how you can implement this technique:

In your controller, include the following function:

  $scope.loadShareThis = function() {
    stButtons.makeButtons();
  }

Next, add the following code to your item-list.html file:

<div ng-repeat="item in vm.itemList" ng-init="loadShareThis()">
    <item-directive item="item"></item-directive>
</div>

Dealing with dynamic URL's may present additional challenges, but that's a separate issue to tackle.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

CSS Grid expands the item width on its own when there is still space left

Having some queries regarding the piece of code provided. Currently, I have a collection of 5 cards displayed in rows of 3 cards each using the styling grid-template-columns: repeat(3, minmax(200px, 1fr));. My concern is whether there is a way for the 4th ...

Obtain the data stored in an object within an array

I am attempting to retrieve the values of objects within an array. const bugSchema = new Schema({ title: { type: String, required: true }, comments:[ { user:{ type: String, required: true }, c ...

Unlocking the Secrets of Laravel Blade Integration in a Script File

I am attempting to create a customized store locator application using the guidance provided in this useful tutorial on Google Maps and Laravel 5. While going through various queries related to integrating Google Maps with Laravel, I came across these info ...

Providing secure access to Apostrophe-CMS S3 assets and attachments via HTTPS

Currently, I am utilizing Amazon S3 to deliver both my static assets and user uploads within the context of apostrophe-cms. While my site is loading via https, all of my assets appear to be loading using http. I have set up a cloudfront distribution at th ...

Ways to invoke a prop function from the setup method in Vue 3

I encountered the following code: HTML: <p @click="changeForm">Iniciar sesion</p> JS export default { name: "Register", props: { changeForm: Function, }, setup() { //How do I invoke the props change ...

When using Node.js, you may encounter the error message: "TypeError: brevo.ApiClient is not a constructor

My goal is to set up an automatic email sending system that, upon receiving details like name and email, will send a confirmation email to the provided email address with the message "subscribed." I've been working on this task for about 7 hours strai ...

Can XMLHttpRequest be exploited for XSS attacks?

Can cross-site scripting be achieved using an XMLHttpRequest as a post method? For instance, in a chatroom where users can enter text. Normally, inserting scripts like <script>alert("test")</script> would be blocked. However, you could write a ...

Identifying line breaks caused by browsers or CSS forced line breaks

<p style="width:60px"> This is just a sample text. It is a piece of text that doesn't really say anything meaningful.</p> When this text is rendered as HTML, it would look like this: This is just a sample text. It is a piece of text ...

What is the best way to retrieve data attributes from multiple div elements that share the same class name?

I have multiple div elements with the same class name. I want to extract the data attribute from these div's. I have tried some code and managed to get the result. However, I am unsure if this is the best approach for retrieving data("cartid"). Any as ...

What is the best way to incorporate arrowheads into the lines that have been sketched using canvas

I need assistance with incorporating arrowheads at the end of linear plots drawn on a coordinate grid using my custom function. You can view an example of the current setup in this JsFiddle: https://jsfiddle.net/zje14n92/1/ Below is the code snippet respo ...

What is the best way to define properties for objects within views.py so that the updated object can be effectively passed to JavaScript code?

When loading an "endless scroll" feed via AJAX and pagination, I realized that before passing objects to the JS code, I need to add a property (or attribute) to every object indicating whether it was liked by the current user or not. However, my initial ...

After clicking on the About page in Vue, my data seems to vanish into thin air

The Vue router is up and running with two pages: Home and About. Everything seems to be functioning properly, however, when I navigate to the About page and then return to the Home page, the data is lost. The page isn't refreshing, I'm simply swi ...

Do we really need to implement ajax in this code snippet?

In my scenario, I have two JSP files named test1.jsp and test2.jsp. The flow of my program goes like this: I need to retrieve data from a textbox in test1.jsp, but the Ajax call is initiated from a different page. My objective is to receive the controller ...

Trouble displaying images from JSON file in AngularJS using ng-src - need help with image loading

While utilizing AngularJS to extract image data from a JSON file, everything seems to work fine for other types of data except images. Why is the ng-src not retrieving any images? I am attempting to load all the images into the div and set the src, alt, a ...

When using WYSIWYG editors, be on the lookout for empty paragraphs and incorrect code in CSS, JavaScript, and

I am currently in the process of redesigning a website for a client. The site is already built using Joomla 1.7, but I am facing an issue with the articles section that was created by the client using a WYSIWYG editor. The problem lies in the messy code st ...

Maintaining awareness of which accordion drawer is currently open within a React application

Just getting started with coding, I recently created a collapsible accordion in a NextJs app using the react-collapse package. Everything seems to be working fine, but I'm running into an issue with keeping track of which 'drawer' is current ...

Javascript - struggling with implementing changeClass() function on click event

I am working on creating movable images within a container using the jQuery plugin found at . My goal is to have items appear within the container when clicked, but I am struggling with changing the class of my object (.item) outside of the container. Th ...

Unexpected Behavior in ComponentDidMount

During my exploration of the React documentation, I came across an example of a clock timer implementation. It was interesting to see how the setInterval function is used inside the componentDidMount method to update the state and trigger re-rendering of ...

The Google Geo Charts fail to load when initiated by Ajax requests

My goal is to use ajax to load an external HTML page that includes the following code: <script type='text/javascript' src='https://www.google.com/jsapi'></script> <script type='text/javascript'> function ...

Developing a MEAN application with simultaneous servers running Express, NodeJS, and Angular 2

Currently, I am trying to establish a connection between my backend (NodeJS, Express) and frontend (Angular 2), but I am facing a hurdle in the process. When I run both servers separately, the client-side and server-side functionalities work correctly. In ...