0

I would need to display different prices for the same item depending on the currency chosen by the user (the different prices are already provided from the backend, so there is no need to do any further conversion). I would like to change the expression with ng-click

My html goes like this:

<div ng-controller="myCtrl">
  you have, {{exchange.[currency]}}...
  <br>
  <br>
  <select>
    <option ngclick="currency='USD'">USD</option>
    <option ngclick="currency='Eur'">Eur</option>
  </select>
</div>

And my controller:

var app = angular.module('myApp', []);
  // controller here
app.controller('myCtrl', function($scope) {

    $scope.currency = "USD";

    $scope.exchange = [{
      "USD": 199,
      "Eur": 20
    }]; 
  })

You can check the code above in a JSfiddle here

1
  • 1
    remove dot on second line html {{exchange.[currency]}} must be {{exchange[currency]}} Commented Apr 24, 2016 at 21:37

1 Answer 1

1

This is how you could do this the Angular way.

HTML:

<div ng-controller="myCtrl">
  you have, {{currency.rate}} {{currency.code}}
  <br>
  <br>
  <select ng-options="currency.code for currency in exchange" ng-model="currency"></select>
</div>

Controller

var app = angular.module('myApp', []);
  // controller here
app.controller('myCtrl', function($scope) {
    $scope.exchange = [{
      "code": "USD",
      "rate": 199
    },
    {
      "code": "EUR",
      "rate": 20
    }]; 
    $scope.currency = $scope.exchange[0];
  })
Sign up to request clarification or add additional context in comments.

1 Comment

That was exactly what I was looking for. Thanks a lot!

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.