javascript - Add an asterisk to a label if field is required - Stack Overflow

I have a label that I want to add an asterisk to if field is required. I have tried applying ng-class l

I have a label that I want to add an asterisk to if field is required. I have tried applying ng-class like so

<div class="col-xs-12 top5"  ng-if="(activity.itemType=='TEXTAREA')">
     <label class="col-xs-3 control-label text-right" for="act4{{$index}}"
            ng-class="{'asterisk-if-mandatory': data[$index].mandatory}">{{activity.itemLabel}}</label>
     <div class="col-xs-9">
         <textarea ng-model="activityItems[$index].value"
                   ng-required="data[$index].mandatory" cols="40" rows="6"
                   id="act4{{$index}}" name="txtComments" class="defaultTextareaInput"></textarea>
     </div>
</div>

and then css

.asterisk-if-mandatory {
    content: " *"
}

Any solutions?

I have a label that I want to add an asterisk to if field is required. I have tried applying ng-class like so

<div class="col-xs-12 top5"  ng-if="(activity.itemType=='TEXTAREA')">
     <label class="col-xs-3 control-label text-right" for="act4{{$index}}"
            ng-class="{'asterisk-if-mandatory': data[$index].mandatory}">{{activity.itemLabel}}</label>
     <div class="col-xs-9">
         <textarea ng-model="activityItems[$index].value"
                   ng-required="data[$index].mandatory" cols="40" rows="6"
                   id="act4{{$index}}" name="txtComments" class="defaultTextareaInput"></textarea>
     </div>
</div>

and then css

.asterisk-if-mandatory {
    content: " *"
}

Any solutions?

Share Improve this question asked Jun 5, 2018 at 13:56 LazioTibijczykLazioTibijczyk 1,9671 gold badge29 silver badges63 bronze badges 3
  • As I don't know angular… What will be the rendered output of ng-class="{'asterisk-if-mandatory': data[$index].mandatory}" ? – Takit Isy Commented Jun 5, 2018 at 13:59
  • 2 put it on an after: .asterisk-if-mandatory:after { content: " *"; display:inline-block; } If your ng class is applied – Pete Commented Jun 5, 2018 at 14:00
  • Great stuff @Pete! – LazioTibijczyk Commented Jun 5, 2018 at 14:01
Add a ment  | 

1 Answer 1

Reset to default 5

I don't really know Angular…
But I would say you can do something like this, using pseudo-element ::after:

label {
  border: 1px solid gray;
  padding: 4px 8px;
}

label.asterisk-if-mandatory::after {
  content: " *";
}
<label class="asterisk-if-mandatory">Item is mandatory</label>
<label class="">Item is not mandatory</label>
<label class="asterisk-if-mandatory">Item is mandatory</label>

(I reduced the code to the minimum required for the snippet)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745096868a4611032.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信