ng 模式不适用于动态添加的角度输入标签

ng-pattern not working for dynamically added input tag in angular

本文关键字:输入 标签 添加 模式 不适用 适用于 动态 ng      更新时间:2023-09-26

我在变量中有一个输入标签,当我将其插入div 时ng-patternng-pattern不起作用

风格:

.from_fields {
    transition: all linear 0.5s;
    background: transparent;
}
.from_fields.ng-invalid {
    color: white;
    background: red;
}

脚本

$scope.input='<input type="text" ng-pattern="/^[0-9]{1,7}$/" class="from_fields">';
angular.element(document.getElementById('test')).html($scope.input);

有人可以帮忙吗??

您尝试输入的字符串需要针对 angular 进行编译,以使其在其监视列表中并评估任何 ng 属性。

现在ng-pattern是一个常规的自定义数据属性,与角度无关

如果您不想绑定在角度控制器中,只需删除 ng-

离开

'<input type="text" pattern="^[0-9]{1,7}$" class="from_fields">';

请注意,模式属性不需要斜杠

否则,您必须在控制器中导入$compile服务

并编译给定$scope变量的字符串

var input = '<input type="text" ng-pattern="^[0-9]{1,7}$" class="from_fields">';
var compiled_input = $compile(input)($scope)

并将其附加到文档中

angular.element(<selector>).append(input);