JavaScript Why does angular differentiate between `<my-element></my-element>` and `<my-element />`?

I just discovered a weird issue with Angular 1.2.1, demonstrated in this fiddle (tested in both IE, FF, and Chrome): if I create a very simple templated directive, it fails to work as <my-element /> if it hasn't first been used as <my-element></my-element>.

This is the js code from the fiddle in its entirety:

angular.module('app', []);
angular.module('app').directive('myElement', function() {
    return {
        restrict: 'E',
        replace: true,
        template: '<p>Hello, element!</p>'
    };
});

and the html:

<div ng-app="app">
    <my-element />
    <my-element></my-element>
</div>

The output I expect from that is two paragraphs with the contents Hello, element!, but I only get one. If, however, I reverse the order of the two <my-element>-tags, so that the self-closing one comes after, both of them give output.

Why does Angular behave in this pathological way?

Answer:1

Your own self-closed tag should not be used

The browser interprets if a tag is "self-closeable" or not.

It doesn't know your tag as "self-closeable" (he knows only the HTML tags) and this is why it has a strange behavior.

On www.w3.org you can find the Elements section:

A void element is an element whose content model never allows it to have contents under any circumstances.

The following is a complete list of the void elements in HTML: area, base, br, col, command, embed, hr, img, input, keygen, link, meta, param, source, track, wbr

A non-void element must have an end tag, unless the subsection for that element in the HTML elements section of this reference indicates that its end tag can be omitted....

So, the conclusion is: don't use the short form for your own tag.

Interpreting your own self-closed tag in browser

That strange behavior appears because the browser interprets a self-closed tag like a parent of all siblings that come after it.

Let's ignore the AngularJS for the moment, and let's consider 3 new tags that are not in HTML: <x>, <y> and <z>.

E.g.:

<x></x>
<y></y>
<z></z>

will appear in the source code (F12 in Chrome) as they are.

But

<x></x>
<y/>
<z></z>

will look like:

<x></x>
<y>
    <z></z>
</y>

Now, they we have this information, let's come back to AngularJS:

<my-element />
<my-element></my-element>

becomes

<my-element>
    <my-element></my-element>
</my-element>

and the "main" = parent <my-element> is found and replaced by <p>Hello, element!</p>. => one output line => in this case you can also have some text or other tags after the 2nd line of code, because they will be wrapped in the parent and the result will be the same.

But, on the other hand:

<my-element></my-element>
<my-element />

will become

<my-element></my-element>
<my-element></my-element>

that will be displayed 2 times.

Answer:2

Here is the issue :-

https://github.com/angular/angular.js/issues/1237

In HTML there is no such thing as a self closing tag. There are only tags which are leafs. Leaf tags are fixed.

Answer:3

I have 2 long URLs that point to the same page, http://preview.tbwabox.co.nz/_v005/index.html#buying-a-car http://preview.tbwabox.co.nz/_v005/index.html#buying-a-house and I would like to check if ...

I have 2 long URLs that point to the same page, http://preview.tbwabox.co.nz/_v005/index.html#buying-a-car http://preview.tbwabox.co.nz/_v005/index.html#buying-a-house and I would like to check if ...

I have a webapp that makes calls to a database. Every once in a while, we get a javascript alert dialog that says that the transaction was deadlocked. Honestly, we don't care if that happens because ...

I have a webapp that makes calls to a database. Every once in a while, we get a javascript alert dialog that says that the transaction was deadlocked. Honestly, we don't care if that happens because ...

  1. javascript alert block execution
  2. javascript alert block
  3. javascript block alert box

I'm trying to use formdata object to send form data to my server. I need this because one of my input fields is a file. However the formdata object is blank when I try to send the data to my server, ...

I'm trying to use formdata object to send form data to my server. I need this because one of my input fields is a file. However the formdata object is blank when I try to send the data to my server, ...

  1. formdata object empty
  2. form data empty object
  3. javascript formdata object empty
  4. formdata append empty object
  5. formdata returns empty object

I have a form, which allows to select an item from a dropdown list and upload a file. The name and the ID of the item are saved in a Spreadsheet document. Works with one file...but I want to upload ...

I have a form, which allows to select an item from a dropdown list and upload a file. The name and the ID of the item are saved in a Spreadsheet document. Works with one file...but I want to upload ...

  1. handling multiple files in python
  2. handling multiple files in vi
  3. handling multiple files in c
  4. handling multiple files in vi editor
  5. handling multiple files using awk
  6. vim handling multiple files
  7. handling multiple feature files in cucumber
  8. sftp handling multiple files simultaneously