如何将窗体中的文本框显式设置为dirty? [英] How to explicitly set a text box in a form to dirty?

查看:82
本文介绍了如何将窗体中的文本框显式设置为dirty?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到类似 Angular.js以编程方式将表单字段设置为肮脏,但它们无法正常工作.

I already saw questions like, How to set a particular field in a form to dirty in angularjs 1.3 and Angular.js programmatically setting a form field to dirty but they are not working.

我正在自动填充angularjs中的文本框,如下所示:

I am auto-filling a text box in angularjs as:

$ scope.address.city =圣塔克拉拉";

$ scope.address.city.$ dirty = true;

在html中,我有 ng-model ="address.city" ng-model-options ="{updateOn:'change blur'}" .

但是, $ scope.address.city.$ dirty = true; 在控制台中给出了 undefined .

However, $scope.address.city.$dirty = true; is giving undefined in the console.

我用过

$ http.get("somewebsite.com").success(function(data){$ timeout(function(){$ scope.address.city.$ dirty = true;},0);console.log('$ scope.address.city.$ dirty',$ scope.address.city.$ dirty);})

但仍然出现错误,因为 TypeError:无法设置未定义的属性'$ dirty'

but still I am getting error as TypeError: Cannot set property '$dirty' of undefined

我正在使用角度1.3.1.

I am using angular 1.3.1 .

推荐答案

在HTML中,

<form name="formName">
    <input type="text" name="city" ng-model="address.city" 
    ng-model-options="{ updateOn: 'change blur' }" />
</form>

在控制器中,

$timeout(function () {
    $scope.formName.city.$dirty = true;
}, 0);

请注意, name 属性不是模型名称.

note that name attributes are used not the model names.

这是演示

这篇关于如何将窗体中的文本框显式设置为dirty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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