2014-02-06 23 views
1

由于Angular-UI-Mask is acting oddly,我使用jquery-inputmask我的一些投入,但是当输入动态插入纽约角就得不到面膜:我们如何在动态插入的输入上设置掩码?

<li ng-repeat="item in items"> 
    <input type="text" name="birth_date" class="span2 format_date" ng-model="birth_date" placeholder="Data de Nascimento" required /> 
</li> 

这是相关的脚本

<script type="text/javascript"> 
    jQuery(document).ready(function(){ 
     $(".format_date").inputmask("99/99/9999"); 
    }); 
</script> 

是有什么我可以做的强制它设置面具新的投入?

回答

1

像jQuery.inputMask这样的jQuery插件可以在文档“准备就绪”的情况下(如您的代码所示)将行为附加到DOM元素。这将运行一次,而不再一次,因此对于动态添加的内容,此方法不起作用。

相反,您需要的东西只要相应的DOM被更改就会运行。因此,无论何时添加“项目”列表中的“项目”,都会添加该元素,并针对该元素运行相应的jQuery函数。你需要为此使用AngularJS,你可以编写自己的指令,但幸运的是,有人已经为你编写代码:jQuery Passthrough plugin作为Angular UI's UI.Utils的一部分。

这是working Plunkr

您需要包括在上面的脚本中,像这样(我downloaded it from GitHub):

<script src="ui-utils.jq.js"></script> 

加载模块到AngularJS,例如:

var app = angular.module('myApp', ['ui.jq']); 

,然后使用该指令您的HTML标记:

<input type="text" ui-jq="inputmask" ui-options="'99/99/9999', { 'placeholder': 'dd/mm/yyyy' }" /> 
+0

精美地工作。谢谢。 –