Having trouble retrieving the value of a custom directive attribute

My custom directive, named "mycomponent", has the following configuration:

restrict: 'AE',     
templateUrl: 'template.html',
scope:{
    sTransactionType: '=transactionType',
    sStorageVariable: '=storageVariable'                
},
link: function(scope, element, attrs){              
    console.log("attrs.transactionType:: "+attrs.transactionType);
    console.log("scope.sTransactionType:: "+scope.sTransactionType);

When using the directive in markup like below:

<my-component transaction-Type="FundTransfer" storage-Variable="FundTransferToday"></my-component>

I face an issue where the values of attributes transaction-Type and storage-Variable are returning as undefined when trying to access them in the link function of the directive.

I have tried changing attribute and scope variable names without success.

How can I successfully retrieve custom directive attribute values into scope variables?

Updated code snippet:

var fundtransfer = angular.module("fundtransfer",['mydirectives']);
var controllers = {};
controllers.cntFundTransfer = function($scope, $rootScope){
}

var mydirectives = angular.module("mydirectives",['Constants']);
mydirectives.directive('myComponent', function($rootScope){
restrict: 'AE',     
templateUrl: 'template.html',
scope:{
    sTransactionType: '=transactionType',
    sStorageVariable: '=storageVariable'                
},
link: function(scope, element, attrs){              
    console.log("scope.sTransactionType:: "+scope.sTransactionType);
}
}

<my-component transaction-type="FundTransfer" storage-variable="FundTransferToday"></my-component>          

Answer №1

It appears that your attribute naming is incorrect.

All attribute names should be in lowercase.

Please consider using the following format:

<custom-element type="fundtransfer" variable="fundtransfertoday"></custom-element>

JavaScript Code

restrict: 'AE',     
templateUrl: 'template.html',
scope:{
    sTransactionType: '=type',
    sStorageVariable: '=variable'                
},
link: function(scope, element, attrs){              
    console.log("scope.sTransactionType:: "+scope.sTransactionType);
}

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 dependency path in the package.json file contains all the necessary files

I recently developed a JavaScript package and here is the configuration in my package.json: { "name": "packageName", "version": "1.0.0", "description": "Description of the package", " ...

Could you confirm if this is a TypeScript function?

Recently, while delving into the vue-next source code, I stumbled upon a particular line that left me puzzled. Due to my limited experience with TypeScript, I found myself struggling to grasp its purpose. Could someone clarify if this snippet constitutes ...

One-Of-A-Kind Typescript Singleton Featuring the Execute Method

Is it feasible to create a singleton or regular instance that requires calling a specific method? For instance: logger.instance().setup({ logs: true }); OR new logger(); logger.setup({ logs: true }); If attempting to call the logger without chaining the ...

Attempting to run the npm install command led to encountering a SyntaxError with an

Recently, I made some alterations to my npm configuration and ever since then, I have been consistently encountering the same error whenever I try to install various packages. It seems that due to a personal mistake in changing the npm settings, I am now u ...

What is the best way to incorporate a dynamic background in NextJS using Tailwind?

I have a poster image that I want to use as a background image, fetched from MovieDb. I tried putting it inside the className like this: className={bg-[url('${path}')] h-screen bg-cover bg-center text-white border-b-8 border-b-solid border-b-sla ...

JavaScript mouse and touch movement events (mousemove, pointermove, touchmove) are not always accurate

I'm currently working on developing a JavaScript whiteboard and have implemented the following code: let lastTimestamp = 0; const fps = 1000/60; document.addEventListener("pointermove", moveMouse, false); function moveMouse (e) { e.preve ...

Troubleshooting problem with Shopify mailto tag

I am facing an issue with external links in my Shopify store. My app injects a script to display a bubble with an anchor tag that redirects users to a specified link. However, Shopify is altering the anchor tag to a different link, resulting in a 404 erro ...

Warning: Typescript is unable to locate the specified module, which may result

When it comes to importing an Icon, the following code is what I am currently using: import Icon from "!svg-react-loader?name=Icon!../images/svg/item-thumbnail.svg" When working in Visual Studio Code 1.25.1, a warning from tslint appears: [ts] Cannot ...

Tips for validating a form with the assistance of jQuery

While there are multiple answers out there for this particular question, I am specifically interested in validating an entire form with dynamic input fields sourced from a database similar to Google Forms. HTML <div class="rs_card"><spa ...

Discover how to efficiently load and display a JSON array or object using JavaScript

I am new to learning about json and d3, having just started a few hours ago. While I have basic knowledge of javascript, I need help with loading a json file and displaying all the arrays and objects on the console using d3. I tried to do it myself but unf ...

Discovering the PHP error message that is sent to AJAX through JQuery

This is the code snippet that works: $.ajax({ type: 'POST', url: 'register.php', data: {captcha: captcha }, success: function() { $('#loading').hide() ...

When attempting to reload a single page application that utilizes AJAX, a 404 error is encountered

Recently, I've been working on coding my personal website and have successfully created a single page application using ajax. The content is dynamically loaded between the header and footer whenever a navigation bar link is clicked. To enable the back ...

Unable to accept the link ID in the modal

Presently, I am facing an issue with a modal that links a product with a customer and involves product discount calculations. The customer is automatically selected using the modal id, and the product is chosen through select options with a while loop. T ...

Delete a class that is not identified by the DOM

Currently, I'm developing an interactive map that triggers an overlay image to highlight the selected area. I am trying to dynamically add and remove classes from a div based on the highlighted area. Initially, I attempted using the starts with selec ...

Using React to display data from a nested JSON object in a table

I am currently working on parsing a JSON object into a table using React. However, I am facing an issue with utilizing the .map() function to create a row for every unique combination of course code, name, transferable_credits, transferable_credits -> i ...

I'm curious, what should the Procfile look like for a node and Angular 2 application?

I am working on deploying my Angular 2 application to Heroku. I have successfully used a Procfile for my Node.js application in the past, where I included the following code: node serverName.js However, I am now facing confusion regarding what should be ...

The route path consists of data that will be retrieved before entering the route

I am facing an issue with my router configuration. I have a dynamic Route that requires the last part of its path to be set after fetching data from a store call in beforeRouteEnter. beforeRouteEnter (to, from, next) { if(to.params.categoryId) { next(vm ...

Non-responsive Ionic 2 Page

On an Ionic 2 page, I encountered an issue where the application becomes unresponsive after pulling data from a service. This makes it impossible to interact with anything on the page, whether on a device, simulator, or in a browser. home.html <ion-he ...

I am puzzled as to why my code in React is rendering twice without any apparent reason

I ran into a strange issue where my console.log("hi") was being displayed twice. I was working on a simple todo-list project and noticed that everything was getting double clicked. After some troubleshooting, it seems like the code is executing any JavaScr ...

Is it possible to save ng-bind values into a PHP string?

As a newcomer to AngularJS with some PHP knowledge, I am curious about the possibility of storing ng-bind values in a PHP string. For instance: <li ng-if="jSEO.resources[3][0][0]"> <span> Internal Links:</span> <font color="#000000" ...