When does 'ng-if="true" + data-ng-if="true" equal false?

It's interesting to note that when utilizing ng-if="truthyValue" and data-ng-if="truthyValue" within an HTML element in the context of AngularJS version +1.6, the element in question will not be rendered in the DOM. This holds true even if ng-if and data-ng-if are used with a boolean value of true.


Is there anyone who understands the reasoning behind this phenomenon?

Answer №1

This particular response draws from various excerpts of AngularJS's directive documentation.


The rationale behind this lies in the fact that AngularJS HTML templates go through a process known as compilation:

Within AngularJS, "compilation" refers to the attachment of directives to HTML elements to render them interactive.

During compilation, template directives are subjected to Normalization, which entails converting case-insensitive names in HTML to camelCase format utilized by AngularJS. This involves the following steps:

  1. Remove x- and data- prefixes from element/attribute names.
  2. Transform colon (:), hyphen (-), or underscore (_) delimited names into camelCase.

As a result of this process, instances may arise where two ngIf directives are linked to a single HTML element. In such scenarios, conflicts can occur (it is explained why having two occurrences of ng-if works but not when mixed with data-ng-if - the browser eliminates one directive before reaching the AngularJS compiler).


Therefore, to avoid such complications, the solution is to refrain from using multiple directives in your HTML that AngularJS normalizes to identical formats.

Answer №2

After conducting some tests, it appears that everything is functioning properly with Angular 1.6.5.

It's important to note that using both data-ng-if and ng-if on the same element is not recommended. Stick to using one of them for consistency.

<div ng-app="myApp">
  <div ng-controller="MyCtrl">
    <div ng-if="false">hello there</div>
    <div ng-if="true">whats up</div>
  </div>
</div>

var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', ['$scope', function($scope) {


}]);

You can view a working example on codepen:

https://codepen.io/anon/pen/YxoXgE

This issue isn't exclusive to ng-if but also applies to other Angular directives like ng-repeat. Simply removing one of the declarations will resolve the problem immediately.

Here's an instance where using both ng-repeat and data-ng-repeat doesn't work: https://codepen.io/anon/pen/KvjdZV

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 the Paginate Pipe effectively retrieve the array length in an Angular 2 application?

I find myself in an interesting situation where I have a piece of code that is functioning within my Angular 2 application - it's generating the correct value, but the method behind its success is unclear to me. Specifically, I am using ng2-paginatio ...

"Unlocking the Power of Node.js and Express for Efficient

I'm relatively new to Express, and I've been researching this issue extensively, but I can't seem to get it right. My goal is to pass an array like this ["1","2","3","4","5"] as a payload fro ...

Is there a way to create a popover menu in VueJS without using JQuery

I found exactly what I need in this helpful answer. It demonstrates a menu appearing when clicking on a table row. The dilemma is that it relies on JQuery... Therefore, I am curious if achieving the same functionality without JQuery is possible. Maybe thr ...

Why is the else block being executed even though the condition in the if block is true in Javascript?

While delving into Node.js, I encountered an issue with my cartData.json file not updating properly. Here's the code snippet in question: routes.post("/cart", (req, res, next) => { let prodId = req.body.productId; let productList = []; let ca ...

Unable to retrieve element using getElementById with dynamically assigned id

After researching on Google and browsing through various Stack Overflow questions (such as this & this), I have not been able to find a solution. I am currently working on validating dynamically generated rows in a table. The loop and alert functions a ...

Filter ng-repeat according to the user's choice

Currently, I am developing a music catalog and working on implementing a feature that allows users to filter the catalog based on a list of artists and genres The list: <ul class="artists"> <li class="title"> <h5> ...

Sending a JSON object from JSP to JavaScript using AJAX

Is there a way to transfer JSON values from JSP to JavaScript as an object using AJAX without resorting to global JavaScript variables, which can expose the JSON content in the page's source? The desired scenario is as follows: The JSP URL is opene ...

Every time I try to use the EJS syntax, particularly when I use the 'include' function, the page just won't load

There seems to be an issue with the page loading - it only works once or twice, if I'm lucky. However, when I remove the EJS syntax from the file and just leave the HTML, it loads without any problems. I can't seem to figure out why this is happe ...

Making a synchronous call to a web API using JQuery

Understanding JQuery promises and deferred objects has been a bit of a challenge for me, so please bear with me. I should also mention that my application is built using React, Typescript, and ES6. Let's imagine we have an array of objects: [{ Objec ...

Troubleshooting problems with scrolling on JavaScript in Edge, Opera, and Chrome

Currently, I am developing a fun CSGO-inspired box opening feature on my food recipe website that randomly selects recipes for users. However, I have encountered some challenges with the scrolling functionality. It seems to be incompatible with certain bro ...

Can you guide me on accessing the base64 string within the base64 object?

I am looking to extract the data that comes after "data: application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,". Specifically, I want to retrieve the data starting with "UEsDBBQACAgIANBmkE8AAAAA ...". How can this be achieved? En ...

Vue-incredible swiper, a pair of components swipe using buttons adjacent to one another

I am currently using vue-awesome-swiper and encountering an issue. The swipers on my page have buttons (prev slide, next slide) for navigating through slides. However, when I include two swipers on the same page, the buttons of one swiper end up affecting ...

Exploring directory organization in GraphQL Queries using GatsbyJS

In my portfolio, I have organized my work into categories, pieces, and pictures in a cascading order similar to a child-parent relationship. The folder structure reflects this hierarchy, with the main problem being explained in more detail below. Folder s ...

Display solely the dates on the timeline chart of Google charts

I am facing an issue with my timeline chart script that I am using. The code snippet is as follows: google.charts.load("current", {packages:["timeline"],'language': 'pt'}); google.charts.setOnLoadCallba ...

"Vue.js allows for seamless updates of parent data from the child component upon key press

My child component has an input field that is supposed to update the data in the parent component when the user starts typing. Below is a snippet of my code, let me know if you need more information. Child <input :keyup="updateFilters(filters[data.f ...

Instead of using <div>, try substituting it with a different HTML page

Seeking assistance on an HTML page project that involves jQuery, CSS, and HTML without server-side integration. Encountering an issue when attempting to replace a <div> with content from another HTML file saved on my computer. The main page code sni ...

What is causing the Access-Control-Allow-Origin error when using axios?

I have a simple axios code snippet: axios.get(GEO_IP) .then(res => res) .catch(err => err); In addition, I have configured some default settings for axios: axios.defaults.headers["content-type"] = "application/json"; axios.defaults.headers.common. ...

Having trouble with a Three.JS/WebGL 3D object not loading in Firefox?

After creating a 3D Object using Three.js examples, I noticed that it works perfectly in Chrome and IE 11, but for some reason, it's not loading on Firefox. I am currently using the latest version of Firefox (FF 27.0). When I tested the code on Firef ...

Guide on incorporating text input areas into specific positions within a string

Looking for a way to replace specific words in a string with input fields to enter actual values? For example... Dear Mr. [Father_name], your son/daughter [name] did not attend class today. This is what I want it to look like... Dear Mr. Shankar, your ...

The orthographic camera method combined with raycasting for object selection

Currently, I'm facing a challenge when it comes to selecting objects with the orthographic camera using the raycaster. Interestingly, I don't encounter any issues when utilizing a perspective camera. The only difference between the two scenarios ...