Using AngularJS ngRepeat to dynamically render a series of iframes by tracking element changes

Currently, I am displaying a series of TinyMCE wysiwygs by utilizing the ng-repeat directive:

<div ng-repeat="widget in wigdets">
   <textarea wysiwyg></textarea> <!-- This is a directive to initialize TinyMCE -->
</div>

Upon rearranging the order of widgets in the controller, the instances of TinyMCE are automatically adjusted as well.

An issue arises due to the fact that the TinyMCE widget functions within an iframe and when it is relocated within the DOM, the iframe's state is reset.

As a solution, I must save the content of TinyMCE, remove TinyMCE from the element before relocating it, then reinitialize TinyMCE and reapply the saved content once the movement is complete.

I am seeking a straightforward method to somehow integrate into ng-repeat and designate callbacks for element movements.

If it becomes necessary to develop my own ng-repeat-custom directive, what would be the ideal structure for organizing event dispatching in AngularJS?

Should events be transmitted to child directives? Or should child directives (such as wysiwyg) subscribe to events from parent directives?

Answer №1

It's important to have open communication about directives from children to parents, ensuring that both parties understand and consider the full spectrum of directives.

Answer №2

The issue at hand really comes down to "how can I trigger an action after ng-repeat has finished manipulating the DOM". After some searching, I stumbled upon a helpful solution in a related query: Calling a function when ng-repeat has finished

In my particular scenario, the controller handles modifications to the widgets list. This allows me to simply use $scope.emit('pre_move') and $scope.emit('after_move') events, which I can then manage within my wysiwyg directive.

An important aspect here is wrapping 'after_move' inside a $timeout() function! By doing so, it ensures that the execution will begin during the next digest cycle, giving you assurance that all DOM manipulations are completed by then.

Controller:

function controller($scope, $timeout) {
    var widget1 = new Widget();
    var widget2 = new Widget();

    $scope.widgets = [widget1, widget2]

    $scope.emit('pre_move'); 

   // Rearrange widgets
   $scope.widgets[0] = widget2;
   $scope.widgets[1] = widget1;

   $timeout(function(){
        // Using $timeout wrapper is crucial to guarantee that the event will be emitted 
        // during the next digest cycle
        $scope.emit('post_move');
   })

}

Wysiwyg TinyMCE directive:

link: function(scope, element, attrs) {
      ...

      var content;
      scope.on('pre_move', function() {
         content = element.html();
         element.tinymce().remove; // remove TinyMCE before rearrangement
      });

      var content;
      scope.on('post_move', function() {
         element.tinymce().html(content); // reinitialize TinyMCE after rearranging
      });
}

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

Guide to utilizing a shared route function across various routes in express.js

Is there a way to handle the scenario where I need both www.example.com/12345/xxxxx and www.example.com/xxxxx to trigger the same function in my application using Express? app.get('/:someVar/xxxxx', function(req, res) { /* etc */ }); I can acce ...

Using an external call to trigger the revert method in jQuery UI

My draggable event setup looks like this: $(ids.label).draggable({ containment: ids.wrapper, revertDuration: 100, revert: function(event) { $(this).data("draggable").originalPosition = { top: $(this).data('origionalTo ...

Utilize cascading styles with React's CSS modules

After encountering a problem in my React app where I had a mix of inline JS styles and CSS classes, I made the decision to separate everything into CSS modules for more organized styling. Now, I import these modules like so: import styles from './Imag ...

I'm having trouble with my controller - not sure what the problem is

My controller seems to be malfunctioning. I have created a controller but it is not functioning properly. Even though I have reviewed it multiple times, the issue persists. Could someone please assist me with this problem? Angular Code var myPanelSearch ...

Applying a custom class to a particular row in an AngularJS datatable using the created

I am currently working with AngularJS datatables and I want to apply a custom class to a row if a specific condition is met. Here's what I have attempted so far: this.dtOptions = DTOptionsBuilder.newOptions() ... .withOption('createdRow', f ...

Challenges with ExpressJS 4 middleware

Trying to grasp the concept of middleware in ExpressJS 4 has been quite a challenge for me. As far as I understand, middleware are applied based on the order they are declared and can be "bound" at different levels. My current focus is on binding a middl ...

Populating HTML elements with JSON data

I need help loading multiple JSON objects into individual divs in an HTML file. Each object should be displayed in a separate div, with the first object going to the first div, the second object to the second div, and so on. I believe I need to use a for l ...

Intercepting $http requests to customize API call URLs

var app = angular.module('app'); // create an interceptor service app.factory('myHttpInterceptor', function($q) { return { 'request': function(config) { return config; } }; }); Currently, ...

iPhone height is not correct

One issue I faced was when trying to view a webpage in landscape mode on iPhones (specifically testing on model SE, but it appears that many people are experiencing the same problem with other iPhone models). I have created a simple example below, consist ...

Struggling to remove quotation marks from a string that was originally an array before being converted?

Here is the code snippet that I am struggling with: users = [{ "userid": "45", "name": "steve" }, { "userid": "32", "name": "john" }]; getuser = users.flatMap(user => user.userid === '32' ? user.name : []); result = getuser.toSt ...

How can I make sure the return statement in getServerSideProps is only executed once all fetching operations are finished?

Currently, I am able to retrieve a person's username and corresponding data object with just one fetch from firebase. Inside this data object, there is a property named "uploads," which contains an array of documentIDs representing posts uploaded by t ...

Guide to loading an image and incorporating it into a canvas using Gatsby's server-side rendering

I encountered an issue where I was unable to create an image using any of the supported types like CSSImageValue, HTMLImageElement, SVGImageElement, HTMLVideoElement, HTMLCanvasElement, ImageBitmap, or OffscreenCanvas in my SSR application with Gatsby. De ...

Can you explain the significance of these vertices in the box geometry in THREE.js?

After creating a box geometry using the code below: const hand1geo = new THREE.BoxGeometry(2, 0.01, 0.2); const material_sidehand = new THREE.MeshBasicMaterial({ color: 0x3cc1b7 }); const sidehand = new THREE.Mesh(hand1geo, material_sidehand); ...

How can I modify the base color of a cone in Three.js?

My cone in the jsfiddle link is currently all blue, but I want to change the color of the square base to red. To do that, I need to alter the color of the two faces that make up the square base. How can this be achieved? View my cone on JsFiddle: http://j ...

Update the WooCommerce shopping cart page automatically upon product removal

After trying to solve the issue of refreshing the cart page in WooCommerce when a product is removed, I came across this helpful question on Stack Overflow: Refresh the page after product remove from cart Woocommerce. Following the provided code snippet th ...

Is it possible to add a tooltip to a div without using any JavaScript or JQuery?

I've been working on designing a form that includes tooltips displayed constantly on the right side to assist users in completing their forms. To achieve this, I have separated each part into Divs and added a tooltip for each one. <div title="This ...

Enhance the color scheme of your collapsed or expanded Bootstrap NAV for mobile devices

Currently working on customizing the navbar using Bootstrap. I've been able to style it perfectly for both desktop and mobile devices in its initial state. The issue arises when attempting to style the navbar in its expanded and collapsed states on m ...

Tips for altering the visibility of a different class on hover using jss

Exploring the features of material ui react Below is my scss code snippet (when hovering over .content, the .replyBtn becomes visible): .content { &:hover { .replyBtn { visibility: visible } } } .replyBtn { visibility: hidden; } ...

The issue of CSS translate3d causing performance slowdowns on mobile devices with large DOM structures

I have implemented OwlCarousel 1.3.3 on a website, similar to the sync example shown on the official owl website (): var owlconfig = { singleItem: true, navigation: false, pagination: false, afterAction: syncCarousels }; $('.image-gallery&ap ...

Issues with the functionality of the WordPress plugin

The issue with Spin360: All scripts are linked in functions.php add_action('wp_footer', 'add_scripts'); function add_scripts() { if(is_admin()) return false; wp_deregister_script('jquery'); wp_enqueue_script ...