简体   繁体   English

AngularJS 表单和空/空值

[英]AngularJS form and null/empty values

I am working with a somewhat dynamic AngularJS form.我正在使用一个有点动态的 AngularJS 表单。 In other words, I am able to add rows of input fields, etc. So my approach was to start with a $scope.formData empty object, to encapsulate all the properties that are bound to both static and dynamic HTML form elements.换句话说,我可以添加输入字段的行等。所以我的方法是从$scope.formData空对象开始,封装所有绑定到静态和动态 HTML 表单元素的属性。

The AngularJS code is as follows: AngularJS 代码如下:

(function() {
    var formApp = angular.module("formApp", []);
    formApp.controller("FormCtrl", function ($scope, $timeout) {
        $scope.formData = {};
        $scope.formData.subscribers = [
            { name: null, email: null }
        ];
        $scope.addSubscriber = function() {
            $scope.formData.subscribers.push({ name: null, email: null });
        };
    });
})();

The HTML for the AngularJS form: AngularJS 表单的 HTML:

<body data-ng-app="formApp">
    <div data-ng-controller="FormCtrl">
        <p>
            Name of Topic: <input type="text" data-ng-model="formData.title" placeholder="enter a title" />
        </p>
        Subscribers:
        <button data-ng-click="addSubscriber()">Add subscriber</button>
        <table>
            <tr>
                <th>Name</th>
                <th>Email</th>
            </tr>
            <tr data-ng-repeat="subscriber in formData.subscribers">
                <td><input type="text" data-ng-model="subscriber.name" placeholder="enter name" /></td>
                <td><input type="text" data-ng-model="subscriber.email" placeholder="enter email" /></td>
            </tr>
        </table>
        <hr style="margin:1em 0;" />
        <p>
            <em>Debug info</em>: {{ formData }}
        </p>
    </div>
</body>

Note the Debug info section at the end, which displays the $scope.formData object and its contents.注意最后的调试信息部分,它显示了$scope.formData对象及其内容。 I have a couple of issues with the way I have implemented this form.我在实施此表单的方式上有几个问题。

  • When the page first loads, there is no formData.title property in the $scope , but since it is bound to the Title input text field, when I start typing a value, the title property gets added to the $scope .当页面第一次加载时, $scope没有formData.title属性,但由于它绑定到 Title 输入文本字段,当我开始输入一个值时, title属性被添加到$scope However when I delete the value in the input text field, the formData.title property still exists in the $scope as an empty string.但是,当我删除输入文本字段中的值时, formData.title属性仍然作为空字符串存在于$scope中。 I suppose this is okay, but I really want to clean up empty or null values when submitting the form.我想这没问题,但我真的想在提交表单时清理空值或空值。 I would like to do it on the client side if it is easy to do so, so the server side code does not have to clean up anything.如果这样做很容易,我想在客户端执行此操作,因此服务器端代码不必清理任何内容。
  • With the dynamic Subscribers section, I can keep adding as many rows as I want, but ultimately, I would like to filter out all empty subscriber objects on the client side.使用动态订阅者部分,我可以根据需要继续添加任意数量的行,但最终,我想过滤掉客户端的所有空订阅者对象。

Does AngularJS have any options for detecting and cleaning null/empty values in the $scope before further processing, such as a $http POST? AngularJS 是否有任何选项可以在进一步处理之前检测和清除$scope中的空/空值,例如$http POST?

Note I have set up a jsFiddle for this example.注意我已经为这个例子设置了一个jsFiddle

Simply use ngModelController $parsers and overwrite the default HTML input element.只需使用ngModelController $parsers 并覆盖默认的 HTML 输入元素。

With this implementation you can have control over the model value all the time.通过此实现,您可以始终控制模型值。 So in your case you can set the model to null whenever the view value is empty String.因此,在您的情况下,只要视图值为空字符串,您就可以将模型设置为 null。

var inputDefinition = function () {
return {
  restrict: 'E',
  require: '?ngModel',
  link: function (scope, element, attr, ngModel) {
    if (ngModel) {
      var convertToModel = function (value) {
        return value === '' ? null : value;
      };
      ngModel.$parsers.push(convertToModel);
    }
  }
};
/**
* Overwrite default input element.
*/
formApp.directive('input', inputDefinition);

Here is the updated JSFiddle: https://jsfiddle.net/9sjra07q/这是更新的 JSFiddle: https ://jsfiddle.net/9sjra07q/

I try and avoid using watchers for performance reasons.出于性能原因,我尽量避免使用观察者。 With that being said, this really isn't as much of an Angular question as it is a JavaScript question.话虽如此,这确实不是一个 Angular 问题,而是一个 JavaScript 问题。 Since you have total control over when the data is passed off to the service I would simply clean it up first.由于您可以完全控制数据何时传递给服务,因此我会先将其清理干净。 This is fairly simple since your data structure is so shallow.这相当简单,因为您的数据结构非常浅。

https://jsfiddle.net/1ua6oj5e/9/ https://jsfiddle.net/1ua6oj5e/9/

(function() {
    var formApp = angular.module("formApp", []);
    formApp.controller("FormCtrl", function ($scope, $timeout) {

        // Remove junkiness
        var _remove = function remove(item) {
            if ($scope.formData.title === undefined || $scope.formData.title === '') {
                delete $scope.formData.title;
            }
        };


        $scope.formData = {};
        $scope.formData.subscribers = [
            { name: null, email: null }
        ];
        $scope.addSubscriber = function() {
            $scope.formData.subscribers.push({ name: null, email: null });
        };

        // Submit to test functionality
        $scope.submit = function() {

            // Remove title if empty
            _remove($scope.formData.title);

            /* Remove name and email if empty.
             * If your list of fields starts to get large you could always
             * just nest another iterator to clean things up.                 
             */

            angular.forEach($scope.formData.subscribers, function(sub) {
                _remove(sub.name);
                _remove(sub.email);
            });

        };
    });
})();
function replacer(key, value) {
    if (value == "" || value == null) {
       return undefined;
    }
  return value;
}

var foo = {foundation: "", model: {year: 2015, price:null}, week: 45, transport: "car", month: 7};
foo = JSON.stringify(foo, replacer);
foo =JSON.parse(foo);
console.log(foo);

Added Watcher on formData,在 formData 上添加了 Watcher,

$scope.$watch('formData',function(n,o){
      if(typeof $scope.formData.title !== 'undefined' && $scope.formData.title === "" ){
        delete $scope.formData.title;
      }
},true);

Updated fiddle: https://jsfiddle.net/1ua6oj5e/6/更新小提琴: https : //jsfiddle.net/1ua6oj5e/6/

For all the dynamic fields you should use angular form validation, you should see this: https://docs.angularjs.org/guide/forms对于您应该使用角度表单验证的所有动态字段,您应该看到: https : //docs.angularjs.org/guide/forms

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

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