Getting an attribute parameter within a directive: A step-by-step guide

I am currently working on injecting the name from the myController scope into the myObj directive as a custom attribute called nameAttr.

Even though I have set name: '=nameAttr' in the directive's scope, it doesn't seem to be functioning correctly.

Can someone please point out what mistake I might be making?

https://plnkr.co/edit/qceVMl0w2gv03ZuQVQb8?p=preview

HTML

<my-obj nameAttr="name"></my-obj>

INSIDE THE 'MY-OBJ.HTML'

<h2>{{ name }}</h2>
<ol>
  <li ng-repeat="(slug, val) in loop">{{ slug }} - {{ val }}</li>
</ol>

ANGULAR

var mod = angular.module('myApp', []);

mod.controller('myController', myController).directive('myObj', myObject);

function myController($scope){
  $scope.name = 'John Smith';
}

function myObject(){
  return {
    restrict: 'E',
    templateUrl: 'my-obj.html',
    scope: {
      name: '=nameAttr'
    },
    controller: function($scope){
      $scope.loop = {
        'one': 'gfshfh',
        'two': '32435'
      };
    }
  };
}

Answer №1

Ensure that in your index.html file, you include the following line:

<my-object name-attribute="name"></my-object>

------------------^

Remember, when using Directives with names in camelCase, they should be written as camel-case in your templates

Answer №2

When working with Angular, it's important to remember that the framework normalizes an element's tag and attribute name in order to match directives. While we typically refer to directives using case-sensitive camelCase (e.g. ngModel), HTML itself is case-insensitive. This means that when referencing directives in the DOM, we use lower-case dash-delimited attributes (e.g. ng-model).

The normalization process involves:

Removing x- and data- from the front of elements/attributes.
Converting :, -, or _-delimited names to camelCase.

For more information on directives, you can check out the documentation here

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

What is the best way to automatically have the first bar in a column highchart be selected when the page loads?

My highchart consists of a simple column. When I click on any bar in the chart, it gets selected. However, I also want the 1st bar to be selected by default. var chart = $('#container').highcharts(); Upon page load, I have obtained this object. ...

Having trouble scrolling to the top in Flickr Search using AngularJS, the results are restricting my movement

I recently followed a tutorial on creating a Flickr Search App with AngularJS (https://www.youtube.com/watch?v=lvGAgul5QT4). I managed to show the search results on my page, but encountered an issue: I couldn't scroll all the way back up to the search ...

The functionality of async.series and async.each is not behaving as anticipated

I am currently working on developing a web scraping tool using nodeJS to extract image URLs from a website's HTML, store them in a cache, and then identify the largest image based on file size. However, I'm facing an issue where the function de ...

Tips for effectively dividing React application components

Jumping into React to build an application seemed like a good idea at first, but now I realize the complexity of it. Let me walk you through my plan and see if it aligns with best practices: The goal is to develop a React App for organizing the subjects I ...

Failed to perform the action using the Angular Post method

Currently, I am exploring the use of Angular with Struts, and I have limited experience with Angular. In my controller (Controller.js), I am utilizing a post method to invoke the action class (CartAction). Despite not encountering any errors while trigge ...

Can you help me pinpoint the tags related to mail?

The email address appears in various locations throughout the page. For instance <div> <p><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a3c2e3c28dd1d6">[email protected]</a></p> </div> ...

Issue with Electron-vue: 'compute:' not functioning as expected

My attempt to create a simple example using the element-ui library was not successful. There are two 'switches' with different active state values: 2 and 1. The values of the switches are supposed to be displayed in <p>{{sw1}}</p> and ...

React: the function is returning as undefined

Description I am encountering an issue with a function in a functional component, as it keeps returning undefined. Despite having all the necessary data defined and accurate within the function including tableData and subtractedStats. This seems to be a ...

The issue here is that "onreadystatechange" in JS Ajax is not defined, even

For the past day, I've been struggling with this issue and going in circles. Any help would be much appreciated :-) Synopsis I'm facing a challenge with asynchronous AJAX calls to CGI using resolver and FQDN variables to obtain DNS resolution ...

Guide to declaring variables using jQuery

Currently tackling a school project, I stumbled upon some online example solutions and managed to decipher most of the code. However, there is one line that has me puzzled. var $newTweet = $('<div class=tweet></div>'); My assumption ...

Keycloak does not support using the updateToken() function within an asynchronous function

In our development of a Spring application with React/Redux frontend, we faced an issue with Keycloak authentication service. The problem arose when the access token expired and caused unexpected behavior in our restMiddleware setup. Here is a simplified v ...

Are the Bootstrap columns arranged in a vertical stack instead of side by side?

I currently have two large columns that are stacked vertically on top of one another. I want them to be side by side, like | 1 | 2 |, so that the user can view both columns simultaneously. I am seeking tips or suggestions on how to resolve this issue. Whil ...

Does using AJAX with jQuery and PHP guarantee that the response will always be in JSON format?

While working on PHP validation with AJAX requests for user-submitted information, I encountered an issue. The problem arises when attempting to convert the JSON response from PHP to a Javascript object, as it throws the following error: "Uncaught SyntaxE ...

Submitting information to an HTML page for processing with a JavaScript function

I am currently working on an HTML page that includes a method operating at set intervals. window.setInterval(updateMake, 2000); function updateMake() { console.log(a); console.log(b); } The variables a and b are global variables on the HTML page. ...

Troubleshooting issue with ng-hide in AngularJS when using multiple filters

I'm struggling to implement two filters using ng-hide. Check out this fiddle where I've showcased the issue: http://jsfiddle.net/czPf6/2/ I have a piece of code that dynamically selects which filter to use based on input values from an input box ...

Display website when clicked

When creating a website similar to , one issue that I am facing is the ability to scroll down before clicking the "proceed as anticipated" button. This feature seems to defeat the purpose of having the button trigger an automated scrolling effect if users ...

Incorporating dynamic JavaScript animations with immersive 360-degree images

I've been exploring ways to replicate a solution similar to this one: Sample During my research, I came across some lightweight jQuery plugins that can enable 360 degree image rotation. However, there are still a few things I haven't figured out ...

AngularJS: Updating data in controller but not reflecting in the HTML view

Within my AngularJS app, I've come across the following code: example.html <div> <p>{{name}}</p> </div> <div> <button ng-click="someFunction()"></button> </div> exa ...

What is a way to automatically run a function at specific intervals in PHP, similar to the setTimeout() function in JavaScript?

I have a JavaScript code snippet that looks like this: setTimeout('$.ajaxCall("notification.update", "", "GET");', 1000); Now, I want to execute the following PHP function every 1000 milliseconds, similar to the above JavaScript code: $notific ...

What are the steps to make React JSX direct to "/profile" and display the profile page?

Throughout my application, I have integrated reach router to facilitate navigation between the different pages. However, I came across a navbar component that I really like and decided to add it to my app. Strangely, clicking on the "to: "/profi ...