我怎么能代表该输入字段AngularJS已经改变 [英] How can I denote which input fields have changed in AngularJS

查看:189
本文介绍了我怎么能代表该输入字段AngularJS已经改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编辑表单(user.html)这使数据的API,但我想避免的形式将所有数据。我喜欢把刚刚更改的项目。

I'm working on an edit form (user.html) that PUTs data to an API, but I'd like to avoid PUTting all the data in the form. I'd like to PUT just the changed items.

我见过的使用与表单时肮脏和质朴的,但是这也适用于在任何形式上的变化。我也看到了使用NG-变化,但我不想触发改变一个动作一个元素,只是表示该改变的元素应该被包含在PUT。

I've seen the use of dirty and pristine when working with forms, but this applies to any change in the form. I've also seen the use of ng-change, but I don't want to trigger an action on a change to one element, just denote that the changed element should be included in the PUT.

任何人找到一种方法来仅表示已更改输入字段?

Anyone found a way to denote only the input fields that have changed?

推荐答案

如果你把投入在格式名称属性,然后给输入名称属性,还可以访问输入的 $质朴属性。

If you put the input in a form with a name attribute and then give the input a name attribute, you can also access the input's $pristine property.

<div ng-controller="MyController">
  <form name="myForm">
    <input type="text" name="first" ng-model="firstName">
    <input type="text" name="last" ng-model="lastName">
  </form>
</div>

app.controller('MyController', function($scope) {
  // Here you have access to the inputs' `$pristine` property
  console.log($scope.myForm.first.$pristine);
  console.log($scope.myForm.last.$pristine);
});

您可以使用 $ scope.myForm。$原始来看看的任何的领域发生了变化,而 $看到表单上的每个输入的原始属性财产,如果的的输入的改变。你甚至可以遍历 myForm会对象(非输入字段对象有pfixed键$ P $一个 $ ):

You can use $scope.myForm.$pristine to see if any fields have changed, and the $pristine property on each input's property on the form to see if that input has changed. You can even iterate over the myForm object (non-input-field objects have keys prefixed with a $):

angular.forEach($scope.myForm, function(value, key) {
  if(key[0] == '$') return;
  console.log(key, value.$pristine)
});
// first, true
// last, false

这篇关于我怎么能代表该输入字段AngularJS已经改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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