如果响应为假,则将 class 更改为 angular

Change class with angular if response is false

如果请求为假,我在如何切换 class 时遇到了一些问题。让我向您展示我的代码和解释。

我的输入是这样的

<div class="form-group">
    <label for="locationName">Location name</label>
    <input type="text" class="form-control" id="locationName"
           ng-model="locationName">
</div>

这是我获取响应的控制器部分

}).success(function(data, status, headers, config) {
    console.log(data, status, headers, config);
}).error(function(data) {
    $scope.locationNameError = data.errors.locationName;
});

所以现在我不想在设置 $scope.locationNameError 时实现我想要的输入 class 必须是 form-group has-error

到目前为止,我认为如果例如,则进行相同的排序,但这行不通

<div ng-if="!locationNameError" class="form-group">
<div ng-if="locationNameError" class="form-group has-error">

我是不是做错了?我不想用尽可能少的代码来实现解决方案。感谢您的帮助。

使用 ng-class 指令而不是两个 ng-if

<div class="form-group" ng-class="{'has-error': locationNameError}" class="form-group">

使用 ng-class 代替 ng-if:

变化:

<div ng-if="!locationNameError" class="form-group">
<div ng-if="locationNameError" class="form-group has-error">

收件人:

<div ng-class="{'has-error': locationNameError}" class="form-group">

Ng-class 可能是您要找的:

<div data-ng-class="{'has-error': locationNameError}">...</div>