简体   繁体   English

AngularJS如何提交未更改的空白表格字段?

[英]AngularJS how to submit untouched, empty form fields?

I've got a problem with a form. 我的表格有问题。 I'm submitting the form and some of the fields don't have to be filled, when the data is send to a restful API I see that angular doesn't pick up the empty, never touched, never changed, input fields. 我正在提交表单,并且不必填写某些字段,当数据发送到一个宁静的API时,我看到angular不会拾取空的,从未触及的,从未更改的输入字段。 But on the server side the field is expected to be present. 但是在服务器端,该字段有望出现。 How can I get angular to send these fields as well? 我如何也可以发送这些字段?

I have two inputs (data.Record.one, data.Record.two), when I fill one and submit the form I'm ending up with this: 我有两个输入(data.Record.one,data.Record.two),当我填写一个并提交表单时,我将得出以下结果:

{
  "Record": {
    "one": "test"
  }
}

I guess this is because angular doesn't "see" untouched and empty fields. 我猜这是因为angular无法“看到”未触及的空白字段。 But I want that: 但是我想要:

{
  "Record": {
    "one": "test",
    "two": ""
  }
}

Here is a demo of what I try: http://plnkr.co/edit/Ky0PQ9U5o1V7IyU9QgkK 这是我尝试的演示: http : //plnkr.co/edit/Ky0PQ9U5o1V7IyU9QgkK

I'm well aware that I could initialize the form with an empty skeleton object but I would really prefer to not have to do that. 我很清楚,我可以用一个空的骨架对象初始化的形式 ,但我真的希望其他人都这样做。 If I need to do that it's just additional code that as a bonus will just introduce an area of potential fail if another dev adds a field in the form but forgets / doesn't know about the skeleton in the JS code. 如果我需要这样做,那么这是额外的代码,如果另一个开发人员在表单中添加了一个字段但忘记了/不知道JS代码的框架,那么额外的好处就是会引入潜在的失败区域。

Angular treats empty fields as empty/null by default. Angular默认将空字段视为空/空。 You can disable this by adding ng-trim="false" on your input fields. 您可以通过在输入字段上添加ng-trim="false"来禁用此功能。

This will not add extra code, only definition on your markup. 这不会添加额外的代码,只会在您的标记上添加定义。

https://docs.angularjs.org/api/ng/input/input%5Btext%5D https://docs.angularjs.org/api/ng/input/input%5Btext%5D

EDIT: I see now what you want. 编辑:我现在看到你想要什么。 Angular takes good care of your model (data). Angular会很好地照顾您的模型(数据)。 Just because you bind an input field to a sub,sub property won't initialize the property with empty string. 仅仅因为您将输入字段绑定到sub,sub属性不会用空字符串初始化该属性。 The input fields does not contain empty string - they contain null which is what you put in them in the first place (by binding them to an uninitalized property). 输入字段不包含空字符串-它们包含null,这是您首先在其中输入的内容(通过将它们绑定到未初始化的属性)。

The feature you want, is for angular to initialize your data.Record with the properties you need. 想要的功能是使用angular初始化数据。使用所需的属性进行记录。 You could make a custom directive for that called initialize-property that would set its model to an empty string. 您可以为该名为initialize-property的自定义指令设置其模型为空字符串。

EDIT: I created the directive but plunker won't let me access due to some cross domain problems. 编辑:我创建了指令,但由于一些跨域问题,punker不允许我访问。 You should be able to use it in your own work though 不过,您应该可以在自己的工作中使用它

myApp.directive('initializeProperty', function() {
  return {
      restrict: 'A',
      link: function (scope, element, attr) {
        element.val('');
      }
  };
});

And then on your input fields use this: 然后在您的输入字段上使用:

One <input name="one" ng-model="data.Record.one" initialize-property ng-trim="false" /><br />
Two <input name="one" ng-model="data.Record.two" initialize-property ng-trim="false" /><br />

replace your app.js code to this one 将您的app.js代码替换为此

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

myApp.controller('TestFormController', ['$scope', '$log', function($scope, $log) {
    $scope.data = {
      Record : {
        "one": "",
      "two": ""
      }
    }
    $scope.add = function() {
        $log.log($scope.data);
    }
}]);

DEMO 演示

UPDATE ! 更新! change your view with this html 用这个HTML改变你的看法

One <input name="one" ng-init="data.Record.one = ''" ng-model="data.Record.one"  /><br /> 
Two <input name="one" ng-init="data.Record.two = ''" ng-model="data.Record.two" /><br />

i have added extra ng-init directive which will initilize your data 我添加了额外的ng-init指令,它将初始化您的数据

DEMO 演示

我只使用ng-init="inputfieldname=''"修复了它

I had the same problem, so then in my research I find this: http://blog.fernandomantoan.com/angularjs-forcando-bind-de-todos-os-campos-no-submit/ 我遇到了同样的问题,因此在研究中我发现了这一点: http : //blog.fernandomantoan.com/angularjs-forcando-bind-de-todos-os-campos-no-submit/

Solve my problem, but I had to edit, thus: 解决了我的问题,但是我必须进行编辑,因此:

.directive('forceBind',  function() {
    return {
        require: '^form',
        priority: -1,
        link: function (scope, element, attrs, form) {
            element.bind('submit', function() {
                angular.forEach(form, function(value, key) {  
                    if(typeof value == 'object'){
                        if (value.hasOwnProperty('$modelValue')) {
                            if (!value.$viewValue) {
                                value.$setViewValue("");
                            }
                        }
                    }
                });
            });
        }
    };
});

So, when the event submit runs, before entering in method the directive force bind. 因此,当事件提交运行时,在进入方法之前强制强制绑定。 I recommend using ng-submit for validation. 我建议使用ng-submit进行验证。 Sorry my english. 对不起,我的英语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM