Javascript 如何使用ng开关更好地验证

Javascript 如何使用ng开关更好地验证,javascript,angularjs,forms,validation,switch-statement,Javascript,Angularjs,Forms,Validation,Switch Statement,我只想一次显示一条错误消息。我正在考虑使用ng开关,它比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

我只想一次显示一条错误消息。我正在考虑使用ng开关,它比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>

这失败有几个原因

当需要一个字符串时,使用ng开关。例如,第一种情况是与字符串文字形式.LastName.$error.required进行比较,而不是与相同名称的对象属性进行比较

相反,ng开关需要一个表达式。唯一与表达式true匹配的情况是字符串literal 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>

这失败有几个原因

当需要一个字符串时,使用ng开关。例如,第一种情况是与字符串文字形式.LastName.$error.required进行比较,而不是与相同名称的对象属性进行比较

相反,ng开关需要一个表达式。唯一与表达式true匹配的情况是字符串literal 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>
你把ng开关的逻辑倒过来了。on保留要计算的表达式,When保留要匹配的内容。你可能不想这样做,但这表明了我的意思:

<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>
你把ng开关的逻辑倒过来了。on保留要计算的表达式,When保留要匹配的内容。你可能不想这样做,但这表明了我的意思:

<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>
正如dnc253所说,您的ng开关逻辑有点不正确。以下内容将为您提供所需的确切功能

HTML

正如dnc253所说,您的ng开关逻辑有点不正确。以下内容将为您提供所需的确切功能

HTML


您得到的错误是什么?您可能需要单引号“background-color”您得到的错误是什么?你可能需要单引号“背景色”来说明问题!!需要吗?如果errObj.required没有的话,这会抓住什么?谢谢,我最后一次用了这个,我想可能会有一个简短的答案。顺便说一句,当你使用$watchobj,func,true时,$watch的深度是多少?我的意思是,它是手表形式。$error.obj1.obj2如果我做$watch'form.$error'@Langdon是的,不需要了。我采取的另一种方法的遗留物。我更新了我的答案以删除,以免引起混淆。@user2167582我相信它与对象最深的子对象一样深。如果出于某种原因,这是一个问题,您可以单独查看这3个字段,但有些信息告诉我,这项工作比这项工作的价值更大,而且不会有太大的性能差异。怎么了!!需要吗?如果errObj.required没有的话,这会抓住什么?谢谢,我最后一次用了这个,我想可能会有一个简短的答案。顺便说一句,当你使用$watchobj,func,true时,$watch的深度是多少?我的意思是,它是手表形式。$error.obj1.obj2如果我做$watch'form.$error'@Langdon是的,不需要了。我采取的另一种方法的遗留物。我更新了我的答案以删除,以免引起混淆。@user2167582我相信它与对象最深的子对象一样深。如果出于某种原因,这是一个问题,您可以单独查看这3个字段,但有些信息告诉我,这项工作比这项工作的价值更大,并且不会有太大的性能差异。