Angularjs需要asteriks [英] Angularjs required asteriks

查看:112
本文介绍了Angularjs需要asteriks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的形式要求,一些领域和一些不属于指令。必填字段标有要求属性。

I'd have a directives with a large form with some fields that are required and some that are not. The required fields are marked with required attribute.

我要pre-挂起所有&LT的投入;跨度类='东西'> *< / SPAN>
基本上是这样的: $(输入[要求])prePEND(小于跨度类='东西'> *< / SPAN>)

I want to pre-pend all the inputs with <span class='something'>*</span> Basically something like this: $("input[required]").prepend(<span class='something'>*</span>)

我仍然有限angularjs的理解点我对我的指令的编译功能,但是当我到达那里我迷路了。

My still limited understanding of angularjs points me to the compile function of my directives but I am lost when I get there.

免责声明:我的直觉告诉我,做这样的事情是不是真的角的方式 - 这是确定,不管我是否最终会使用这种技术或不是我想知道该怎么做。这就是说我欢迎更多的角的建议为好。

Disclaimer: my gut feeling is telling me that doing something like that is not really "the angular" way - that's ok, regardless of whether I end up using this technique or not I'd like to know how to do that. That said I welcome more "angular" suggestions as well.

感谢您!

推荐答案

每当DOM操作进入谈话,一个指令是要走的路。你想prePEND一个 * 来使用要求属性的任何投入,所以要创建从所需属性的指令。事情是这样的:

Whenever DOM manipulation enters the conversation, a directive is the way to go. You're wanting to prepend a * to any inputs with a required attribute, so you want to create a directive from the required attribute. Something like this:

myModule.directive("required", function() {
   return {
       restrict: 'A', //only want it triggered for attributes
       compile: function(element) {
          //could add a check to make sure it's an input element if need be
           element.prepend("<span class='something'>*</span>");
       }
   }
}

这篇关于Angularjs需要asteriks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆