Angular - form.$dirty 在 $compile 后没有更新
Angular - form.$dirty not updating after $compile
我有一个附加到输入字段的指令,该指令在其 Link 方法中调用 $compile(element)(scope);
。一切正常,除非尝试使用以下内容:
ng-class="{ 'has-error' : frm.first_name.$invalid && frm.last_name.$dirty }"
$invalid 属性 更新,但 $dirty(和 $pristine)始终保留其初始值。
我不确定如何解决这个问题。有人有什么想法吗?任何信息,将不胜感激。谢谢!
你需要使用编译,而不是像这样 link:
app.directive('inputDataType', ['$compile', function ($compile) {
var compileFn = function(element) {
element.removeAttr('input-data-type');
// Add pattern
element.attr('ng-pattern', '^[a-zA-Z0-9]+$');
// Make input field required
element.attr('required', 'true');
// Compile to attach new directives / pattern / required
var fn = $compile(element);
return function(scope){
fn(scope);
};
};
return {
restrict: 'A',
scope: false,
terminal: true,
priority: 1001,
compile: compileFn
};
}]);
查看此处了解更多信息:creating a new directive with angularjs
已更新 plunkr:http://plnkr.co/edit/85uHw9pSS3dEy9dGX2lz
我有一个附加到输入字段的指令,该指令在其 Link 方法中调用 $compile(element)(scope);
。一切正常,除非尝试使用以下内容:
ng-class="{ 'has-error' : frm.first_name.$invalid && frm.last_name.$dirty }"
$invalid 属性 更新,但 $dirty(和 $pristine)始终保留其初始值。
我不确定如何解决这个问题。有人有什么想法吗?任何信息,将不胜感激。谢谢!
你需要使用编译,而不是像这样 link:
app.directive('inputDataType', ['$compile', function ($compile) {
var compileFn = function(element) {
element.removeAttr('input-data-type');
// Add pattern
element.attr('ng-pattern', '^[a-zA-Z0-9]+$');
// Make input field required
element.attr('required', 'true');
// Compile to attach new directives / pattern / required
var fn = $compile(element);
return function(scope){
fn(scope);
};
};
return {
restrict: 'A',
scope: false,
terminal: true,
priority: 1001,
compile: compileFn
};
}]);
查看此处了解更多信息:creating a new directive with angularjs
已更新 plunkr:http://plnkr.co/edit/85uHw9pSS3dEy9dGX2lz