我正在尝试根据它是否为必需字段和是否为空字段向我的元素添加一个类。不知道我做错了什么。我的 console.log 正在打印,但没有分配类。
这是我的html
<div ng-repeat="supplier in vm.exportSuppliers" class="row">
<div class="col-xs-5">
<label ng-show="vm.exportSuppliers[$index].exportSupplier" class="control-label" for="es{{$index}}+Ref">Agreement Reference *</label>
<label ng-hide="vm.exportSuppliers[$index].exportSupplier" class="control-label" for="es{{$index}}+Ref">Agreement Reference</label>
<input class="form-control" id="es{{$index}}+Ref" type="text"
ng-model="supplier.agreementReference" ng-change="vm.addExportSupplier()"
ng-blur="vm.requiredField('es'+$index+'+Ref')"
ng-required="vm.exportSuppliers[$index].exportSupplier">
</div>
</div>
我的功能是确保字段为必填项且未填写
vm.requiredField = requiredField;
function requiredField (id) {
if($scope.vm.exportSupplier.form.$error.required) {
for (var i = 0; i < $scope.vm.exportSupplier.form.$error.required.length; i++) {
if ($scope.vm.exportSupplier.form.$error.required[i].$$attr.id == id) {
console.log('invalid required field');
var myEl = angular.element( document.querySelector( id ) );
myEl.addClass('top40');
}
}
}
}
我的 addClass 有什么问题?
最佳答案
看看下面的问题。我认为您可以通过使用 ng 类中的三元运算符来改进您的方法。
关于javascript - 从 Controller 动态添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50485090/