简体   繁体   English

验证div中动态生成的输入

[英]Validate dynamically generated inputs inside a div

I'm trying to validate dynamically generated inputs, but I do not know how to do this. 我正在尝试验证动态生成的输入,但是我不知道该怎么做。

When I add the div that triggers the directive and inserts the inputs dynamically the div adds the 'has-error' class but does not apply the input style , anyone knows the best way to do this that I'm trying to do? 当我添加触发指令并动态插入输入的div时,div添加了“ has-error”类,但不应用输入样式有人知道我尝试执行此操作的最佳方法吗?

Here is the markup: 这是标记:

<div ng-if="conditionItem.field.id"
     ng-class="{true: 'has-error'}[conditionItem.field.hasError]"
     dynamic
     input-router
     source="conditionItem.field"
     ng-click="FieldConditionsCtrl.valueTest(conditionItem.field.hasError)"
     ng-required="true"
     ng-model="conditionItem.situation[$index]">
</div>

Here is the directive how generate the inputs: 这是伪指令如何生成输入:

(function() {
  'use strict';

  angular
    .module('applicationInputs', ['rzModule', 'focus-if', 'ui.utils.masks'])
      .directive('inputRouter', inputRouter);

    /** @ngInject */
    function inputRouter($compile){
        return {
            restrict: 'EA',
            scope: {
                ngModel: '=',
                source: '=',
                placeholder: '@',
                tabIndex: '='
            },
            link: function(scope, element, attrs) {
                var canvas = angular.element(element[0]);
                scope.source.editable = angular.isUndefined(scope.source.editable) ? true : scope.source.editable === true;

                //used by setting to override selected field
                if (angular.isDefined(attrs.dynamic)) {
                    scope.$watch('source', function () {
                        var html = '<' + scope.source.type + 'input></' + scope.source.type + 'input>';
                        canvas.children().detach();
                        canvas.append($compile(html)(scope));
                    });

                } else {
                    var html = '<' + scope.source.type + 'input></' + scope.source.type + 'input>';
                    canvas.append($compile(html)(scope));
                }
            }
        }
    }
})();

Here is my style: 这是我的风格:

.has-error {
     border-color: red
}

Try border: 1px solid red; 尝试边框:1px纯红色; instead of just setting border-color. 而不是仅设置边框颜色。 Border width is 0 by default, so just setting a color isn't enough 边框宽度默认为0,因此仅设置颜色是不够的

Just a couple nitpicky-subjective style comments also: 只是一些挑剔的主观风格的评论也:

  • element is already a jqLite/jquery element, so no need to call angular.element 元素已经是jqLit​​e / jquery元素,因此无需调用angular.element
  • the scope.source.editable === true assignment can be shortened to !!scope.source.editable if you really want it to be a boolean. 如果确实希望将布尔值指定为scope.source.editable ===,则可以将真正的赋值缩写为!! scope.source.editable。
  • While clever, this sort of jquery style element building is generally a code smell in angular js. 虽然巧妙,但这种jQuery样式元素的构建通常是Angular js中的代码味道。 If you really want to go this route, I would build self-contained directives for your inputs and use inputRouter's template to choose. 如果您真的想走这条路,我将为您的输入构建独立的指令,并使用inputRouter的模板进行选择。 It's easier to understand, so your future self will thank you 更容易理解,所以您未来的自我会感谢您
  • {true: 'has-error'}[conditionItem.field.hasError] took me a minute. {true:“发生错误”} [conditionItem.field.hasError]花了我几分钟。 Just write it as {conditionItem.field.hasError: 'has-error'} 只需将其写为{conditionItem.field.hasError:'has-error'}

I'm a big fan of these style guides for AngularJS: 我非常喜欢AngularJS的这些样式指南:

There's overlap, but take what you like from each. 有重叠,但是从每个中取您想要的。

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

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