Employ a directive within a different directive

I have developed a directive for displaying SVG icons and now I want to use this icon directive within another directive.

Icon directive:

<icon p="shopping-add"></icon>

What I am aiming for:

app.directive("product", function() {
  return {
      restrict : "E",
      require: 'ngModel',
      scope:{
        ngModel:"="
      },
      template : '<div><icon p="iconName"></icon></div>'
  };
});

How can I achieve the nested directive functionality?

Answer №1

Follow this example for better understanding.

let app = angular
  .module('MyApp', [
  ])
.controller('Main', ['$scope', function ($scope) { 

}])
.directive("product", function() {
  return {
      restrict : "E",
      template : '<div><icon image="https://example.com/image.jpg"></icon></div>'
  };
})
.directive("icon", function() {
  return {
      restrict : "AE",
      scope :{
        image:'@'
      },
      template : '<img src="{{image}}" />'
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
 <div class="main-content" ng-app="MyApp" ng-controller="Main as myCtrl">
      <div>
           <product ></product>
     </div>
</div>

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

Sorting the Czech alphabet with the help of Tablesorter

Utilizing the tablesorter characterEquivalents extension as outlined in this documentation. I have created a similar extension for Czech characters, but the sorting is not functioning correctly for certain characters - like \u017d $.extend( $.tables ...

Saving a jimp resized image in a mongoDb database: A step-by-step guide

Does anyone have advice on saving a resized image using Jimp directly to MongoDB database in a Node.js project with Express, Multer, and GridFS? The write method I am currently using only writes to a folder. ...

Incorporating fresh components and newly defined attributes in Angular

Is there a way for me to click on a new component button, specify a name, description, select type, and add attributes such as default value and type? I need all this information to be saved and for the new button to appear in the drag-and-drop section. ...

Encountering a bug that states "TypeError: Cannot read properties of null (reading 'useState')" while trying to use useState in a react application

I'm working on incorporating useState into my Next.js app, but I encountered an error as soon as I added the line of code to initialize useState. The popup error message reads: TypeError: Cannot read properties of null (reading 'useState') ...

Send a JavaScript variable to Twig

I am trying to pass a JavaScript variable to a twig path but the current method I am using is not working as expected. <p id="result"></p> <script> var text = ""; var i; for (varJS = 0; varJS < 5; varJS++) { text += "<a href= ...

Restructure an array of objects into a nested object structure

I have a list of task items that I need to organize into a structured object based on the ownerID var tasks = [ {taskID: "1", title: "task1", ownerID: "100", ownerName: "John", allocation: 80}, {taskID: "2", title: "task2", ownerID: "110", ownerNam ...

Creating a three-row CSS layout where the middle row aligns to the right side

I am working on developing a mobile device layout with 3 blocks. The first and third blocks contain text fields, while the second block is filled with a map. However, all of my blocks don't look good when they are too wide. The browser window can have ...

Passing parent HTML attributes to child components in Angular 2

Is there a way to pass HTML attributes directly from parent to child without creating variables in the parent's .ts class first? In the sample code below, I am trying to pass the "type=number" attribute from the parent to the app-field-label component ...

Exploring Node.js: Uncovering the Secrets of Checking Dependency Versions

How can I dynamically retrieve the version of an external dependency in JavaScript without manually specifying it? ...

Tips for creating a backup static file for angular-translate

When utilizing the translateUrlLoader to retrieve the resource file from the server, what steps should be taken if this process fails? Is there a way to seamlessly switch to using a local file instead? ...

HTML and Javascript Form Integration for Sage Pay Purchase Button

Currently, I am working on a project that includes online payment options via PayPal and Google Wallet, with a "buy now" button. Now, my next step is to incorporate the Sage Pay "buy now" button onto the website using HTML and JavaScript. Although I have ...

Having trouble with Javascript/ajax/php: data is successfully sent from server to client, but client to server communication is not working as

Apologies for the duplicate post (Admins, kindly remove the other one!). I've been receiving great assistance from you all and was hoping to seek your help once again with the following question: I am currently working on implementing AJAX by allowin ...

Why is my JavaScript method not working and throwing an error?

Attempting to streamline my code by creating a separate method for calling an ajax function in multiple places. When I try to use the separate method instead of calling it directly, it doesn't work as expected. data: columns[5], type: 'a ...

Aggregating multiple parameters in a D3 Treemap visualization

I have come across an interesting challenge while working on a project involving a zoomable treemap. I am currently exploring how to pass and aggregate multiple parameters within the treemap, similar to what is typically done in a zoomable treemap visualiz ...

Getting ngSanitize to function properly with the <iframe> tag

I am currently developing a solution using AngularJS, where ngSanitize is being utilized to sanitize the markup retrieved from an Ajax call. There are times when this markup may include iframe tags. During testing, I have noticed that ngSanitize appears ...

Testing the functionality of a video player using Protractor

Exploring the world of front end development is a new adventure for me, so please bear with me. I have created an HTML page that includes an embedded video using the HTML video tag as shown below: <video id=... class=...> <source src=... type ...

What is the best way to capture the input value upon pressing the "Enter" key?

My first question here is about implementing the addtodo(todo) code. After trying it out successfully, I wanted to make it work when typing and pressing enter. However, despite attempting some other methods, I couldn't get it to work. I didn't re ...

Protractor performs the afterEach function prior to the expectations being met

I am completely baffled by the behavior of this code. Everything seems to be executing correctly up until the 'expect' statement, at which point it abruptly navigates to a new page and runs the afterEach function. I've tried various solution ...

Is AngularJS more than just a view?

My goal is to create a single-page Angular app with a central view surrounded by tools like a sidebar and top bar. However, when the user is not logged in, I want the view to display a login partial while hiding the side- and top bars. Additionally, ther ...

Unlocking the Power of Typescript and ReactJS: Maximizing Efficiency with Previous State

I'm encountering difficulties implementing the previous state in React 18 with Typescript version 4.8.3. Here is my refreshToken code and I'm receiving the following error: Value of type '(prev: any) => any' has no properties in c ...