简体   繁体   English

有额外条件的角度脏检查

[英]Angular dirty checking with extra condition

I'm wanting to use ng-dirty to apply a style to form elements in an angular app; 我想使用ng-dirty在样式应用中应用样式来形成元素; however, there is another condition I want to compare as well to determine if the dirty styling should be applied. 但是,还有一个条件我也想比较一下,以确定是否应使用肮脏的样式。 In other words, apply the dirty styling if ng-dirty is true and another condition is true (scope.myVar != null or something like that). 换句话说,如果ng-dirty为true 并且另一个条件为true(scope.myVar!= null或类似的东西),则应用肮脏的样式。

I realize I can accomplish this with ng-class as well but wanted to know if there were alternates. 我意识到我也可以使用ng-class来完成此操作,但我想知道是否有替代方法。

Instead of editing the ng-dirty class, I would add a conditional ng-class to your input field. 无需编辑ng-dirty类,而是将条件ng-class添加到您的输入字段中。 In the case that you're checking your data ( myVar for example) is not null ( $scope.myVar != null ), I would do something along the lines of: 如果您要检查数据(例如, myVar )不为空( $scope.myVar != null ),我将按照以下方式进行操作:

<input ng-model="myVar" ng-class="{ 'custom-condition' : myVar != null }"/>

Then, in your CSS, you would see if BOTH classes (ng-dirty, and your custom-condition ) were currently applied to the element. 然后,在CSS中,您将看到当前是否同时将两个类(ng-dirty和您的custom-condition )应用于该元素。

// If both .ng-invalid and .custom-condition are applied:
.ng-dirty.custom-condition { 
    border: 1px solid red; 
}

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

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