angularMoment cannot be triggered from other component?


#1

Hello All,

I have a problem with using angularMoment. I am not able to changeLocale of moment from other Component. Let me explain abit. I have a main Component as follows,

import angular from ‘angular’;
import angularMeteor from ‘angular-meteor’;
import uiRouter from ‘angular-ui-router’;
import ‘angular-translate’;
import ‘moment’;
import ‘moment/locale/tr’;
import ‘moment/locale/en-gb’;
import angularMoment from ‘angular-moment’;

import { Meteor } from ‘meteor/meteor’;

import template from ‘./app.html’;

class App {
constructor($scope, $reactive) {
‘ngInject’;

    $reactive(this).attach($scope);
}

}

const name = ‘app’;

export default angular.module(name, [
angularMeteor,
uiRouter,
Navigation,
‘pascalprecht.translate’,
angularMoment,
]).component(name, {
template,
controllerAs: name,
controller: App
})
.config(config)
.run(function(amMoment, $rootScope, $state) {
‘ngInject’;

    amMoment.changeLocale('tr');

    $rootScope.$on('$stateChangeError',
        (event, toState, toParams, fromState, fromParams, error) => {
            if (error === 'AUTH_REQUIRED') {
                $state.go('login');
            } if (error === 'ALREADY_LOGGED_IN') {
                $state.go('posts');
            }
        }
    );

});

function config($locationProvider, $urlRouterProvider, $translateProvider) {
‘ngInject’;

$locationProvider.html5Mode(true);

$urlRouterProvider.otherwise('/');

$translateProvider.preferredLanguage('tr');

}

This initiates locale language as TR as default. Everything works perfect here. But what I want is to change the locale from other component named of Navigation which is as follows,

import angular from ‘angular’;
import angularMeteor from ‘angular-meteor’;
import uiRouter from ‘angular-ui-router’;
import ‘angular-translate’;
import ‘moment’;
import ‘moment/locale/tr’;
import ‘moment/locale/en-gb’;
import angularMoment from ‘angular-moment’;

import { Meteor } from ‘meteor/meteor’;

import template from ‘./navigation.html’;

class Navigation {
constructor($scope, $reactive, $state, $translate, amMoment, $rootScope){
‘ngInject’;

$reactive(this).attach($scope);

this.state = $state;
this.translate = $translate;
this.amMoment = amMoment;
this.rootScope = $rootScope;
}


changeLanguage (languageCode) {
    if (languageCode) {
        this.translate.use(languageCode);
    } else {
        this.translate.use('en');
    }
    this.amMoment.changeLocale(languageCode); // Which is not working
    this.rootScope.$digest();
};

}

const name = ‘navigation’;

export default angular.module(name, [
angularMeteor,
uiRouter,
‘pascalprecht.translate’,
angularMoment
]).component(name, {
template,
controllerAs: name,
controller: Navigation
});

I cannot change the locale of it :frowning: The following code not working.

this.amMoment.changeLocale(languageCode); // Which is not working.

What should I do ? Please help me :(…

Thanks in advance for your helps in advance.