Starting an HTML attribute in Angular with the letter "x" is not permitted

After working with Angular 1.5.6 components, I came across a peculiar discovery. I have a component named scale which I call using the following syntax:

<scale x-scale="xScale"></scale>

In my controller, I set:

$scope.xScale = 'lin'.

Here's how I defined my component:

angular
    .module('myapp')
        .component('scale', {
            templateUrl: 'analyse/components/scales/scale.tpl.html',
            controller: function(){
                console.log('in controller and this is ', this);

            },
            bindings: {
              xScale: '='
            },
    });

Surprisingly, when checking the console log, it showed undefined.

However, by simply changing x-scale to r-scale in the template and updating xScale in the binding to rScale, everything worked perfectly. Strangely enough, substituting the letter x with any other also made it work flawlessly. What could be causing this behavior?

Answer №1

The information regarding directives can be found in the guide dedicated to directives.

Normalization

Angular performs normalization on an element's tag and attribute name to match them with directives.
Directives are usually referenced using their case-sensitive camelCase normalized names (e.g. ngModel).

Although HTML is not case-sensitive, directives are referred to in the DOM in lowercase format, often using dash-separated attributes on DOM elements (e.g. ng-model).

The normalization process unfolds as follows:

  1. Remove x- and data- prefixes from element/attribute names.
  2. Convert the name separated by :, -, or _ into camelCase.

Angular removes the x- prefix from the beginning of any attribute name for normalization purposes. This is because both standard data-attributes, starting with data-, and x-attributes, starting with x-, are valid in HTML 5.

As per the HTML5 specification:

Attributes starting with "x-" are reserved for use by user agents and will never be formally added to the HTML language.

The specification also mentions that:

Extensions intended for use with HTML syntax should be limited to new attributes in the form "x-vendor-feature", where vendor denotes the name of the extending party and feature represents the feature itself.

While x- attributes may not see much usage, they are designated for browser vendors according to specifications. It is advised against using them; instead, opt for data-attributes. Angular will automatically eliminate the data- part for you as well. Thus,

<scale data-scale="scale"></scale>
<scale x-scale="scale"></scale>
<scale scale="scale"></scale>

all hold the "same" meaning when applying

$scope.scale = 'lin'.

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

Using jQuery effects on your PHP message to give it an interactive touch - here's how!

For some time now, my website has had a straightforward PHP message system that worked well, storing data into MySQL with each message having a unique ID. Recently, out of the blue, I decided to enhance the system by adding jQuery effects for sending and d ...

Efficiently writing to response from MongoDB in JSON format without blocking the execution

My current setup involves using Node/Express.js to create a GET route that fetches data from a Mongo database. While I have successfully implemented this in a non-blocking manner, I encounter a syntax error when attempting to return the data in JSON format ...

Inspect the variable in JavaScript and then increment it by one every second

I am facing an issue where I need to check a variable. If it's 0, then I need to increment it after 1.5 seconds. If it's 10, then I need to increment it after 0.4 seconds. It's quite complex and the current code implementation is not working ...

When the button is clicked, a modal will pop up

Looking for help in incorporating JavaScript to create a responsive modal that pops up with lyrics when a button is pressed in a table. Any assistance would be greatly appreciated. Note: Only the code for the table has been provided. Let me know if you&ap ...

Duplicate the canvas onto a new canvas

One of the functions I am using involves copying an old canvas to a new canvas. Here is the code snippet for this functionality: function cloneCanvas(oldCanvas) { //create a new canvas var newCanvas = document.createElement('canvas&a ...

The display/block feature will only function if the div element is contained within a table

I am facing an issue with hiding/showing two div elements alternatively. The code works perfectly when the divs are placed within a table, but fails when they are not in a table due to compatibility issues with Internet Explorer. I prefer not to use a tabl ...

Updating checkboxes in Vue.js

I'm struggling a bit with VueJS states. Here is the setup for my simple app: new Vue({ el: '#media-app', data: { activeTypes: [], activeCategories: [], medias: [] }, methods: { getFilteredDat ...

How is it that a callback function can successfully execute with fewer arguments provided?

Code I'm really intrigued by how this code functions. In the verifyUser function, it passes a callback function as the fourth argument to the verifyToken function. However, upon examining the verifyToken function itself, I noticed that it only has th ...

A guide on replacing certain characters with spaces using AngularJS

I'm struggling with replacing special characters in a name (Inka -Tiitto-s-Camp-Aero-Gravity-Milan-9) with hyphens (-) due to an error message that says, "The URI you submitted has disallowed characters." Additionally, the name appears as (Inka Tiitto ...

Tips on incorporating an AJAX post request in your code

Hello, I'm in need of assistance to solve a problem. Here is an example of a site that uses jQueryUI: http://jqueryui.com/sortable/#connect-lists Below is the code: <!doctype html> <html lang="en"> <head> <meta charset="utf-8" ...

The fluid jumbotron in react-bootstrap is not extending to the entire width of the webpage

For the past few days, I've been engrossed in working on a website to sharpen my React skills using create-react-app, react-bootstrap, and styled-components. Initially, everything was smooth sailing with the jumbotron when it resided in my App.js file ...

Generating VueJS Syntax from JSON Data

My goal is to retrieve locale language variables from a JSON Request generated by Laravel and load them into VueJS. VueJS does not natively support locales, so I am facing issues with the ready function alert not working while the random text data variable ...

Phonegap app failing to run Ajax request properly

I have recently updated my Android app using phonegap build to the latest version, cli-5.2.0. Here is a snippet of my config: <preference name="phonegap-version" value="cli-5.2.0" /> Additionally, here is how my overall configuration looks like: & ...

Obtain the position of a value within an array that has been grouped together

In my JavaScript code, I am dealing with two arrays. One array contains Ids: [1,1,1,2,2,2,3,3,...] The other array holds values: [0,1,0,0,0,1,1,0,...] What I need to do is determine the zero-based index of the group of Ids where the corresponding value ...

Interactive tooltip feature in Apexchart

I need to include % in my Apexcharts tooltip after the Y value. Working with vue.js and without official documentation from apexchart, I managed to make it function correctly. This is what I have accomplished so far: data: function () { return { ...

Navigating through this object with PUG and Express: a step-by-step guide

I am currently working on a basic blockchain project to practice my skills with nodejs. I have created a blockchain object that consists of a block class, and now I want to loop through this object using pug. app.get('/', function(request, respon ...

How to modify browser's back button action in Vue.js

Is there a way to detect the back function in the browser and then redirect to a different page using Vue.js? Here's what I've tried so far. mounted: function () { window.onpopstate = function(event) { this.$router.push({ path: ...

Unable to create an intersection between two arrays that are nested within each other

I'm currently trying to make translations dynamic, but I'm facing a challenge when it comes to displaying the corresponding objects from another array. In this scenario, I have two arrays that share the same model. What I aim to achieve is to in ...

Utilizing Rails' remote: true feature to prevent the webpage from refreshing

Currently, I have a 'projects/show.html.erb' page where a project has many 'project_messages'. Users can successfully create new project messages on this page. However, there is an issue where the page refreshes when creating a new proj ...

How can I retrieve the element to which the jQuery plugin was attached?

After working on my first jQuery plugin, I encountered some issues with adding CSS in the head. Inline CSS was causing bugs in IE8 - 10 when injected by the script, so I need to use a styles tag instead. The challenge is targeting the actual element that ...