【问题标题】:how to use ng switch to better validate如何使用 ng switch 更好地验证
【发布时间】:2013-05-22 17:14:40
【问题描述】:

http://jsfiddle.net/x3azn/AbmsG/1/

我只想一次显示一条错误消息。我正在考虑用ng switch 来做这件事,它比ng:show 代码效率更高,但是我现在拥有的算法目前不起作用。

<div class="errorDiv" ng-switch on="true">
    <div ng-switch-when="form.LastName.$error.required" style="color: white; background-color: red">required</div>
    <div ng-switch-when="form.LastName.$error.len" style="color: white; background-color: red">len</div>
    <div ng-switch-when="form.LastName.$error.dallas" style="color: white; background-color: red">dallas</div>
</div>

【问题讨论】:

  • 您得到的错误是什么?您可能需要单引号“背景颜色”

标签: javascript angularjs forms validation switch-statement


【解决方案1】:

你的 ng-switch 逻辑倒退了。 on 保存要评估的表达式,whens 保存要匹配的内容。您可能不想这样做,但这说明了我的意思:

<div class="errorDiv" ng-switch on="form.LastName.$error.required">
    <div ng-switch-when="true" style="color: white; background-color: red">required</div>
</div>
<div class="errorDiv" ng-switch on="form.LastName.$error.len">
    <div ng-switch-when="true" style="color: white; background-color: red">len</div>
</div>
<div class="errorDiv" ng-switch on="form.LastName.$error.dallas">
    <div ng-switch-when="true" style="color: white; background-color: red">dallas</div>
</div>

http://jsfiddle.net/AbmsG/3/

【讨论】:

    【解决方案2】:

    失败有几个原因。

    ng-switch-when 需要一个字符串。因此,例如,第一种情况是与字符串文字“form.LastName.$error.required”进行比较,而不是同名的对象属性。

    相比之下,ng-switch 需要一个表达式。与on 表达式true 匹配的唯一情况是字符串文字“true”。

    虽然没有您想要的确切行为,但这会起作用:

    <div class="errorDiv" ng-switch on="form.LastName.$error.required">
        <div ng-switch-when="true" style="color: white; background-color: red">required</div>
    </div>
    <div class="errorDiv" ng-switch on="form.LastName.$error.len">
        <div ng-switch-when="true" style="color: white; background-color: red">len</div>
    </div>
    <div class="errorDiv" ng-switch on="form.LastName.$error.dallas">
        <div ng-switch-when="true" style="color: white; background-color: red">dallas</div>
    </div>
    

    【讨论】:

      【解决方案3】:

      正如 dnc253 所说,您的 ng-switch 逻辑有点不对劲。以下将为您提供所需的确切功能。

      http://jsfiddle.net/ud3323/AbmsG/4/

      HTML

      <form ng-app="someApp" name="form" ng-controller="MainCtrl">
        <input validate name="LastName" ng-model="form.lastName" dallas len = "5" required />
        <div class="errorDiv" ng-switch on="currentError">
          <div ng-switch-when="required" style="color: white; background-color: red">required</div>
          <div ng-switch-when="len" style="color: white; background-color: red">len</div>
          <div ng-switch-when="dallas" style="color: white; background-color: red">dallas</div>
        </div> 
      </form>
      

      JS

      angular.module('someApp', [])
        .directive('validate', function() {
        return {
          require: 'ngModel',        
          link: function(scope, element, attrs, ctrl) {
            element.bind("keydown keypress", function(event) {
                if (event.which === 13) {
                    scope.$apply(function() {
                        scope.$eval(attrs.onEnter);
                    });
                    event.preventDefault();
                }
            });
            ctrl.$parsers.push(function(val){
                if (!val) val = '';
                ctrl.$setValidity('required',
                                   val != '');
                ctrl.$setValidity('len',
                                    val.length == 5);
                  ctrl.$setValidity('dallas',
                         val=='dallas');
                  return val;
              });
          }
      }
      }).controller('MainCtrl', ['$scope', function ($scope) {
        $scope.$watch('form.$error', function (errObj) {
          if (errObj.required) $scope.currentError = "required";
            else if (errObj.len) $scope.currentError = "len";
            else if (errObj.dallas) $scope.currentError = "dallas";
        }, true);
      }]);
      

      【讨论】:

      • !!errObj.required 怎么了? if (errObj.required) 没有捕捉到什么?
      • 谢谢,我上次用过这个,想可能会有更短的答案。顺便说一句,当您执行 $watch(obj, func, true) 时,$watch 会走多远?我的意思是如果我做 $watch('form.$error') 它会看 form.$error.obj1.obj2
      • @Langdon 是的,不再需要那个了。我采取的另一种方法的结转。我更新了我的答案以删除,以免引起混淆。
      • @user2167582 我相信它和对象最深的孩子一样深。如果由于某种原因这是一个问题,您可以单独查看 3 个字段,但有些事情告诉我,这比这值得做的工作更多,并且不会有太大的性能差异。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-28
      • 1970-01-01
      相关资源
      最近更新 更多