Why isn't nesting directives working in AngularJS?

I am facing an issue with one parent directive and multiple child directives.

My requirement is to nest one directive inside another, resulting in the use of multiple directives.

Could someone assist me in identifying any errors in this code or provide examples that meet our requirements?

JS Fiddle Link : http://jsfiddle.net/Lcch85a1/

Angular JS Directive Code

bosAppModule.directive('layoutView',function($compile){
    var layoutObj={};

    linkFn=function(scope, element, attributes, controller) {
        console.log("Layout : ");
        console.log(scope.layoutData);
        $compile(element.contents())(scope);
        //var containerString = "<layoutContainerView layout-data='layoutObject.Data' container-data='containers' ng-repeat='containers in layoutData.collections.container.rowSet'>Hello container  {{containers.containerId}}   </layoutContainerView>";
        //var compiledElement = $compile(containerString)(scope);
        //element.append(compiledElement);
    };
    layoutObj.scope={layoutData:'='};
    //layoutObj.transclude='true';
    layoutObj.restrict='E';
    //layoutContainerObj.replace='true';
    layoutObj.template="<div id='{{layoutData.collections.layout.rowSet[0].layoutId}}' layout-data='layoutObject.Data'>Hello layout     {{layoutData.collections.layout.rowSet[0].layoutId}}</div>";
    layoutObj.link = linkFn;

    return layoutObj;   
});

bosAppModule.directive('containerView',function($compile){
    var layoutContainerObj={};

    linkFn=function(scope, element, attributes, controller) {
        $compile(element.contents())(scope);
        console.log("Container : ");
        console.log(scope.layoutData);
        console.log(scope.containerData);
    };
    layoutContainerObj.scope={layoutData:'='};
    //layoutContainerObj.transclude='true';
    layoutContainerObj.restrict='E';
    //layoutContainerObj.replace='true';
    layoutContainerObj.template="<div >Hello container  {{containers.containerId}} </div>";
    layoutContainerObj.link = linkFn;

    return layoutContainerObj;  
});

HTML Source Code:

<layout-view layout-data='layoutObject.Data'>
    <container-view layout-data='layoutObject.Data' ng-repeat='containers in layoutObject.Data.collections.container.rowSet'></container-view>
</layout-view>

Answer №1

According to your code on JSFiddle - Example

Code Snippet

<layout-view>
</layout-view>

JavaScript Section

layoutObj.template="<div>Hello layout <container-view ></container-view>  </div>";

Answer №2

If you're looking for a solution to a common issue, check out this page here. Many have asked similar questions in a more concise manner with simpler scenarios.

Here is the response from @rgarcia...

To ensure your directive runs before ng-repeat and can make modifications when it clones the element, set a higher priority than the default ng-repeat priority of 1000.

You can find an explanation on how ng-repeat works in the Directives user guide under the "Reasons behind the compile/link separation" section.

Setting priority: 1001 to your directives should resolve the issue.

Add priority: 1001 to your directives like shown below...

bosAppModule.directive('layoutView',function($compile){
    var layoutObj={};
    priority: 1001

    linkFn=function(scope, element, attributes, controller) {
        console.log("Layout : ");
        console.log(scope.layoutData);
        $compile(element.contents())(scope);
    };
    layoutObj.scope={layoutData:'='};
    layoutObj.restrict='E';
    layoutObj.template="<div id='{{layoutData.collections.layout.rowSet[0].layoutId}}' layout-data='layoutObject.Data'>Hello layout     {{layoutData.collections.layout.rowSet[0].layoutId}}</div>";
    layoutObj.link = linkFn;

    return layoutObj;   
});

To further abstract the directive, you can nest it as follows...

Template .html Document

<custom-tag ng-repeat="..."></custom- tag>

Create This Directive:

bosAppModule.directive('abstractedDirective',function(){ restrict: "E", templateUrl: "path/to/template/foo.html" });

Update Your HTML

<layout-view layout-data='layoutObject.Data'>
    <abstracted-directive></abstracted-directive>
</layout-view>

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

The Antd table documentation mentions that rowKey is expected to be unique, even though it appears they are already

Having trouble with a React code issue. I have a list of products, each with an array of 7 items that contain 40 different data points. This data is used as the source for a table. {label : someStringLabel, key: someUniqueKey, attribute1: someInt,..., at ...

The onchange event does not have any effect

One of my selects, which I'll refer to as A, triggers an ajax call that loads options into a separate select, referred to as B, within a div when an option is chosen. I've implemented some onchange Ajax code for select B, but it doesn't see ...

Disable Jquery toolstrip while the menu is open

Currently, I am utilizing the jQuery toolstrip plugin in my project. I have a requirement to disable it whenever the sidebar menu is opened. Below are the HTML codes for my menu with li tags: <div class="sidebar-wrapper" id="sidebar-wrapper"> <ul ...

Best Practices for Retrieving Data with Django - Post Requests

I'm attempting to use Ajax to send a JavaScript array to Django. Here's my code: document.getElementById('input-generate').onclick = () => { // Get the token const csrftoken = document.querySelector('[name=csrfmiddlewar ...

Error: Uncaught ReferenceError: d3 is undefined. The script is not properly referenced

Entering the world of web development, I usually find solutions on Stack Overflow. However, this time I'm facing a challenge. I am using Firefox 32 with Firebug as my debugger. The webpage I have locally runs with the following HTML Code <!DOCTYP ...

Generating PDF files utilizing AJAX technology with FPDF

I have a PHP script that generates a PDF file using the FPDF library. When I run the script directly, it successfully creates and opens the PDF in the browser. However, when I try to trigger this generation process through a button click using AJAX, it doe ...

Concealing a DisplayFor component with the help of Jquery

Hey there, I currently have a 'td' element that appears like this <td style="font-weight:bold"> @Html.DisplayFor(x => x.Parts[i].QtyInItem, new { htmlAttributes = new { @class = "qtyInItem" } }) </td> A ...

What is the best way to configure routes for a Single Page App with Express.js?

I am currently in the process of developing a single page application using Express.js. On my index.html file, I have included a simple form that, upon submission, will send a request to an API, fetch the data, process it, and then display the parsed infor ...

Arrangement of Events in an AngularJS Timeline

https://i.sstatic.net/qtUZb.jpgI am currently working with angularjs version 1.5.0 and I am trying to adjust the layout of my timeline so that all events are displayed on a single side. Does anyone have suggestions or solutions on how I can achieve this? ...

submitting numerous forms using AJAX

On my webpage, each entry in the database has its own form that saves data upon pressing the enter key. However, I am facing an issue where only one specific form is being saved and not all of them. How can I differentiate between the forms and ensure that ...

The dropdown feature in the bootstrap4 navbar causes the navigation items to shift and adjust their

I am currently working on a navigation bar using Bootstrap4 and I've added a nav-item button to dropdown a menu. However, the issue I'm facing is that when the menu drops down, it pushes other nav-items around, causing them to change position. Th ...

PHP variables that are constantly changing

I recently developed a website that showcases random entries from a database to viewers. Here is the current code snippet I am using: $records = "SELECT * FROM xxx"; $records_query = mysql_query($records); $num_records = mysql_num_rows($records_query); $i ...

Simulate mobile double tap screen resize using either jQuery or Javascript

UPDATE: I have encountered an issue that requires assistance. I am seeking help with implementing a functionality to automatically resize screen contents on mobile devices by calling a DoubleTap. The reason behind this requirement is that when a script up ...

Verifying that the code runs only once the changes to a monitored property have been successfully implemented in the user interface

When working with AngularJS, how can one ensure that code is executed only after a change to a watched property has fully taken effect in the UI? For instance, imagine that the visibility of a loading indicator is tied to the value of an isLoading propert ...

Creating a unique custom selector with TypeScript that supports both Nodelist and Element types

I am looking to create a custom find selector instead of relying on standard javascript querySelector tags. To achieve this, I have extended the Element type with my own function called addClass(). However, I encountered an issue where querySelectorAll ret ...

Is it possible to import a component from a production build of create-react-app?

Has anyone tried importing a component from a production build of create-react-app? Situation: I have one CRA project that has been built for production. Inside this project, there is a component named ExampleButton. Now, I am working on a second CRA pro ...

Typescript encountering onClick function error during the build process

My current challenge involves creating a submit function for a button in my application. However, when I attempt to build the project, I encounter a typing error that is perplexing me. Despite trying various methods, I am unable to decipher how to resolve ...

Is the format of the ISOString valid?

How can I verify if a field is in ISOString format? It works fine when I input a valid date such as const create = '2018-08-02T02:07:49.214Z', but it causes an error in the script when I use const create = 'b'; Here is an example: ...

Is it possible to adjust the height of a panel in Jquery Mobile 1.4.5?

Currently working on a jqm project that is specifically for mobile devices. In this project, I have implemented two panels - one set to push and the other overlay. While one panel is positioned in the left corner of the screen, the other is located in the ...

I am hoping for JavaScript to perform three distinct actions depending on the names of the files that are currently open in Photoshop

Currently, I am in the process of developing a JavaScript script for Photoshop. The main objective of this script is to prompt the user to choose a folder containing multiple files. Subsequently, the script will open each file within the folder and perform ...