0

I have a form where the credit card type is detected when typed.

The type of card is in the field "paymentForm.cardNumber.$ccEagerType" and I can show it using:

{{paymentForm.cardNumber.$ccEagerType|lowercase}}  --> 'visa', 'maastercard'....

if I use ng-class="'visa'" in my form all works well.

But I ´d like to use

ng-class="'{{paymentForm.cardNumber.$ccEagerType|lowercase}}'"

and this is not working.

What am I doing wrong?

<form class="form-horizontal" role="form" name="paymentForm" novalidate>
    <div class="form-group">
                <label class="col-sm-3 control-label" for="card-number">Card</label>
                <div class="col-sm-6">
                  <input type="text" class="form-control input-lg"  
                  ng-class="'visa'" ng-model="card.number" cc-number cc-eager-type
                  name="cardNumber" ui-credit-card-mask id="cardNumber" placeholder="Cartao Visa, Mastercard e Discover">
                </div>
    </div>
</form>

//css

.visa {
          background-image: url('....
}
.mastercard{
          background-image: url('....
}
1
  • try removing the single quotes from ng-class so that you have ng-class="{{paymentForm.cardNumber.$ccEagerType|lowercase}}" Commented Jan 26, 2018 at 14:26

2 Answers 2

2

You don't need ng-class for this, just output into class directly:

<form class="form-horizontal" role="form" name="paymentForm" novalidate>
    <div class="form-group">
        <label class="col-sm-3 control-label" for="card-number">Card</label>
        <div class="col-sm-6">
        <input type="text" class="form-control input-lg {{paymentForm.cardNumber.$ccEagerType|lowercase}}" ng-model="card.number" cc-number cc-eager-type
            name="cardNumber" ui-credit-card-mask id="cardNumber" placeholder="Cartao Visa, Mastercard e Discover">
        </div>
    </div>
</form>

ng-class is useful if you have something like this:

<div
    ng-class="{
        'even': $even && someOtherConditions,
        'odd': $odd && someOtherConditions
    }">
    ...
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it in two different ways.

Either as a predefined string with a statement:

<div ng-class="{'green': class === 'green', 'red': class === 'red'}">class: {{class}}</div>

Or you use the scope prop directly:

<div ng-class="class">class: {{class}}</div>

function TestCtrl($scope) {
  $scope.class = 'red';

  $scope.switch = function() {
    if($scope.class === 'green') {
    	$scope.class = 'red';
    } else {
    	$scope.class = 'green';
    }	
  };
}
.green {
  color: green;
}
.red {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
  <div ng-controller="TestCtrl">
    <div ng-class="{'green': class === 'green', 'red': class === 'red'}">class: {{class}}</div>
    <div ng-class="class">class: {{class}}</div>
    <button ng-click="switch()">Switch</button>
  </div>
</div>

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.