简体   繁体   中英

Angular directive to append asterisk in label text

As found out in SO, we can create a custom directive to append required validation asterisk for an element as specified below:

mod.directive("mandatory", function() {
       return {
           restrict: 'A',
           compile: function(element) {
              element.after("*");
           }
       };
    });

I've one simple label tag as follows:

<label mandatory>Name</label>

What it does is it creates the following html content as follows:

 <label mandatory>Name</label>*

What I want is the following html content:

<label mandatory>Name*</label>

I've tried the .text, .html, .appends functions of the element, but it didn't reflect any changes.

Any suggestions will be appreciated.

You may want to set the text in this way:

mod.directive("mandatory", function() {
       return {
           restrict: 'A',
           compile: function(element) {
              element.text(element.text() + "*");
           }
       };
    });

However, instead of manipulating the element directly, I would opt for a template or something like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM