简体   繁体   中英

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>

if i can understand you problem based on values first select box you want to populate second select box. the below Plunker will solve your problem

plunker link http://plnkr.co/edit/nND1iw7unDnelGQ3EtnA?p=preview

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM