Search code examples
javascriptangularjsionic-frameworkangular-translate

Angular if condition is not working with states


I am using angular translate in an Ionic app, and depending upon the language selected in the drop-down, different states need to be loaded.

Main Controller:

.controller('mainCtrl', function ($scope, $state, $translate) {
  var ctrl = this;
  ctrl.language = 'kn';
  ctrl.languages = ['kn', 'en'];

  ctrl.updateLanguage = function () {
    $translate.use(ctrl.language);
  };

  $scope.lang = function () {
    if (ctrl.language = 'kn') {
      $state.go('knmenu', {}, { location: "replace", reload: true });
    } else($translate.use(ctrl.language)) {
      $state.go('menu', {}, { location: "replace", reload: true });
    }
  }

main.html:

<div ng-controller="mainCtrl as ctrl">
  <button
    class="button button-block button-balanced"
    ng-click="lang()"
  >
    {{ 'TITLE' | translate }}
  </button>
  <select
    ng-options="language | translate for language in ctrl.languages"
    ng-model="ctrl.language"
    ng-change="ctrl.updateLanguage()"
  ></select>
</div>

Solution

  • In you condition check if (ctrl.language = 'kn') this is always true, it should be if (ctrl.language == 'kn') I guess this is the problem. Hope this will help.