1

I'm having some problems how to switch class if request is false. Let me show you my code with explanation.

So this is how my input looks like

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

And this the part of controller where i fetch the response

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

So now i wan't to achieve that when $scope.locationNameError is set my class on that desired input has to be form-group has-error

So far i was think to make same sort if for example, but this is not working

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

Am i doing this the wrong way? I wan't to implement the solution with the less code possible. Thank you for your help.

1
  • 2
    What about using ng-class? Commented Jun 23, 2016 at 16:00

3 Answers 3

4

Use ng-class instead for ng-if:

Change:

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

To:

<div ng-class="{'has-error': locationNameError}" class="form-group">
Sign up to request clarification or add additional context in comments.

2 Comments

omg, it so easy :D lol. i'm amberesd a little bit :D Thank you
Nice to help you :)
2

Ng-class is probably what you're looking for:

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

Comments

2

Use ng-class directive instead of having two ng-if

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.