Angular directive within a directive

I'm a beginner in AngularJS and I'm attempting to create a directive that contains another directive inside it.

Here is how the first directive looks:

(

function () {
    app.directive("cmEventBar", function () {

        var controller = function () {
            var vm = this;
            vm.events = [
                {name: "Truckdriver1", eventId: 1 },
                {name: "Truckdriver2", eventId: 1 },
                {name: "Truckdriver3", eventId: 1 }
            ];
        }

        return {
            templateUrl: "app/shared/eventsBar/eventBar.html",
            restrict: "E",
            controller: controller,
            priority: 1000,
            terminal: true,
            controllerAs: "vm",
            scope: {}
        };
    });
})();

Within the html of this directive, I am using ng-repeat to display all events. Inside ng-repeat, I have another directive:

<div ng-repeat="item in vm.events">
            <cm-single-event name="{{item.name}}" eventId="{{item.eventId}}"></cm-single-event>
        </div>

Here is how the second directive looks:

(function () {
    app.directive("cmSingleEvent", function () {

        var controller = function () {
            var vm = this;
            vm.info = switchEvent(eventId);
         }

        return {
            template: "<li class={{vm.info.itemClass}}> {{vm.name}} {{vm.info.message}} </li>",
            restrict: "E",
            controller: controller,
            controllerAs: "vm",
            scope: {
                name: "=",
                eventId: "="
            }
        };
    });
})();

The output is a bit strange because the event has 3 elements, but in the output I only see one element, and there is no vm.info from the directive inside.

Output

{{vm.name}} {{vm.info.message}}

What am I doing incorrectly here?

Thank you in advance!

Answer №1

When using the = operator for the variables name and eventId, do not include {{}} when assigning their values to attributes. Also, make sure that the cmSingleEvent directive has bindToController: true set in order to properly retrieve values from the isolated scope to the controller.

<cm-single-event name="item.name" event-id="item.eventId"></cm-single-event>

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

How does Socket.IO manage the functions that are provided to it?

This project involves the use of angularJs, nodeJs, and mongoDb. On the client side, I have the following code: webSocket.emit('createNode', node, function(node){ /* ^ */ /* ...

How can you show a green check mark next to an input field in AngularJS after inputting valid data?

I am diving into the world of AngularJS and Angular Material with my web application. As a beginner in AngularJS and Angular Material, I need some help. My current task is to display a green checkmark next to an input field only when valid data is entere ...

Tips for designing a personalized payment page in PayPal for flexible one-time and subscription-based payments

How can I display a PayPal checkout page with custom fields such as tax and total amount when a user makes a payment for a custom amount? Can multiple fields like sales tax and total amount be added? In addition to that, our web application already has Pa ...

I am curious to understand the reasons behind the occurrence of this ID problem

When I format my code like this const title = document.getElementById("title"); function handleClick() { title.style.color = "red"; } title.addEventListener("click", handleClick); everything works fine. However, if I c ...

Unable to set custom claims using Firebase Auth's setCustomClaims() method

I am encountering an issue with setting custom claims for Firebase Authentication service's token. I am using a Cloud function to establish the custom claims for Hasura. The cloud function is triggered upon the creation of a new user to set the custom ...

What is the best way to display the letter X (Clear) in the Chrome INPUT field with the type

A custom application was developed that utilizes an input field of the number type: <input type="number" min="-1000000" max="1000000" step="0.01"> Within Internet Explorer, there is a small "clear" or [X] button on the right side of the input field ...

When attempting to declare a functional component in React utilizing styled-components in TypeScript, an error is encountered stating "No overload matches this call."

Playground https://codesandbox.io/s/typescript-type-checking-question-0b42t Sample Code type BadgeTypes = { success: string; secondary: string; alert: string; text: string; }; type Theme = { fonts?: object; borderRadius: string; primary?: o ...

Guide on Generating Dynamic JSON to Set and Retrieve Values for Forms and Displaying the Bound Values

I'm a beginner in Ionic 3 and I'm having trouble creating a page that redirects to another page without validation. I want to display the data on the new page, but it's not working. Please help! I also want to create a dynamic JSON object a ...

Experience seamless navigation with Highstock's fluid panning feature

I'm attempting to achieve seamless panning on a basic highstock chart by clicking and dragging within the plot area. Interestingly, I have found that this works flawlessly when my data is not based on timestamps: data: [-25.1,-23.8,-19.9,-19.1,-19.1 ...

Identifying Errors in Meteor's Data Publications

I am currently working on a web application using Meteor and AngularJS 2. Take a look at the publication function below: Meteor.publish('abc', function () { // For throwing the meteor error according to the condition if(!this.userId) throw new ...

Issue with custom Javascript not executing in Internet Explorer versions 9 and 10

Initially, this script is found in the document's head section. <script> document.addEventListener("DOMContentLoaded", function () { var e, t = document.querySelectorAll("div.bounceInDown"); for (var n = 0, r = t.length; n & ...

Central alignment of div with cursor

I'm experimenting with creating a unique custom cursor using a <div> that trails the movement of the mouse pointer. While the current setup works smoothly, I've noticed that when scrolling down the page, the div lags behind until the scrol ...

Building dynamic charts using JSON data in Oracle JET

I am attempting to populate a pie chart using JSON data retrieved from restcountries.eu/rest/v2/all. I use $.getJSON to fetch the data, create a temporary array as the data source, and then bind it to the pie chart. However, I seem to be encountering an er ...

"Notification: The marker element has been eliminated" encountered while attempting to restore text ranges using Rangy within a Vue component

I have a rather intricate Vue component that includes a contenteditable div. My goal is to highlight words within this div using the Rangy library and add extra markup while retaining this markup even after editing the text. Initially, I planned on asking ...

What is the best way to check if an object exists in an array in JavaScript and update it if it does, otherwise add it as a new object in

I am working with an array of objects const target = [{name: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89fafbe2c9eee4e8e0e5a7eae6e4">[email protected]</a>', selected: false, alertType: 'max&ap ...

Can React-Select be utilized in the browser directly from the CDN?

Is it possible to utilize react-select directly in the browser without using bundlers nowadays? The most recent version that I could find which supports this is 2.1.2: How to import from React-Select CDN with React and Babel? In the past, they provided r ...

Confirm the email address using the autocomplete feature in the field

I'm currently utilizing Material UI to design an autocomplete field with multiple inputs that gives users the option to either choose from existing email addresses or input their own. An example of what I'm trying to achieve can be seen here: ht ...

Using jQuery, compare the values of two elements and update the class based on the comparison outcome

My objective is to retrieve data from a database, place it into an <li> element, and then highlight the larger of the two values obtained from these HTML <li> elements. I have created a jsfiddle, but I am uncertain about how to use the addCla ...

How to properly declare an explicit injector when using the resolve parameter in $routeProvider?

$routeProvider resolve feature in AngularJS allows for injecting additional dependencies to the controller function. How can we combine this with explicit dependency injection declaration? Example: angular.module('myModule', []) .config(func ...

Instructions on how to implement a page redirect using Angular UI Router while ensuring the back button is disabled

I am facing a challenge where I need to redirect users to the next state in my application after they submit a form on my site, using Angular UI Router. The goal is to prevent users from being able to use their back button or refresh button to go back or r ...