Sateesh K
Sateesh K

Reputation: 1091

ES6 Syntax for Angular .config() and run()

I am trying to convert my app to use ES6 Syntax and in my main module i have config and run invocations.

I changed them to

import * as angular from 'angular';
import {config,run} from './my-config';
import MyAppController from './my-app-controller';

module.exports = angular.module("my-app", [])

    .config(config)

    .run(run)

    .controller('myAppController', MyAppController)

and here is my-config.js

import MyService from './my-service';
export function config($compileProvider, $logProvider, localStorageServiceProvider,$stateProvider, $urlRouterProvider){

    console.log('.config() : START ');
    $compileProvider.debugInfoEnabled(true);

    $logProvider.debugEnabled(true);

    localStorageServiceProvider.setPrefix('myapp');
    localStorageServiceProvider.setNotify(true, true);

    $stateProvider.state('home', {
        url: '/',
        views: {
            'main': {
                controller: 'myAppCtrl',
                templateUrl: 'app/home.tpl.html'
            }
        }
    });
    // handle routes here
    $urlRouterProvider.otherwise('/');
}
config.$inject =['$compileProvider', '$logProvider', 'localStorageServiceProvider','$stateProvider', '$urlRouterProvider'];



export function run(MyService) {
    console.log('.run() : ');
    MyService.start();
 }

 run.$inject=['MyService'];

And config and run functions are not getting invoked..

Please let me know what i am missing

Thanks

Upvotes: 2

Views: 5096

Answers (2)

BERGUIGA Mohamed Amine
BERGUIGA Mohamed Amine

Reputation: 6290

Bellow I'll write an example explain how to write with ES6 and Angularjs the syntax of Config.

we suppose that our bootstrap module named : main so we need to add this module

main.js

import angular   from 'angular';
import ngRoute   from 'angular-route';
import MainRoute from './main.route';//import config rout whuch I added in //other js file
    let mainModule =
        angular.module('mainApp', [
                'ngRoute',
                homeModule.name,
            ]

        )
        .config(MainRoute);

    export default mainModule; 

main.route.js

  export default  function routeConfig($routeProvider) {

            $routeProvider
                .when('/', {
                    templateUrl: 'src/content.components/home/home.tpl.html',
                    controller: 'HomeController as vm'
                });
        }

    routeConfig.$inject = ["$routeProvider"];    

Upvotes: 1

rrd
rrd

Reputation: 5957

Few things I suggest:

import angular from 'angular';

export default angular
  .module('app', [
    //list of your modules
  ])
  .config(config)
  .run(run)
  .name;

Config we use:

export default function config(/* injectables here */) {
  //Your code
}

Run we use:

export default function run(/* injectables here */) {
 /* @ngInject */
 //Your code
}

You might not use the injectables like we do, or use /* @ngInject */

Upvotes: 5

Related Questions