简体   繁体   English

Angular指令在标签文本中附加星号

[英]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: 正如在SO中所发现的,我们可以创建一个自定义指令来为元素添加所需的验证星号,如下所示:

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: 它的作用是创建以下html内容,如下所示:

 <label mandatory>Name</label>*

What I want is the following html content: 我想要的是以下html内容:

<label mandatory>Name*</label>

I've tried the .text, .html, .appends functions of the element, but it didn't reflect any changes. 我已经尝试过.text,.html,.appends元素的功能,但没有反映任何更改。

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. 但是,与其选择直接操作元素,不如选择模板或类似的模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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