Error injecting module to delete history in ionic

0

Trying to erase the history of an app made with ionic create a new module, but trying to inject it generates the following problem

  

ionic.bundle.min.js: 40 Uncaught Error: [$ injector: modulerr] link $ injector / modulerr? p0 = unicesarApp & p1 = Erro ...% 2F% 2Flocalhost% 3A8100% 2Flib% 2Fionic% 2Fjs% 2Fionic.bundle.min.js% 3A74% 3A503)

index.html

    <script src="lib/ionic/js/ionic.bundle.min.js"></script>
    <script src="lib/ngCordova/dist/ng-Cordova.js"></script>
    <script src="cordova.js"></script>
    <script src="js/app.js"></script>
    <script src="App/login.js"></script>
    <script src="App/historial.js"></script>

login.js

angular.module('unicesarApp', ['ionic', 'historialApp'])
       .controller('formulario', formulario)
       .service('obtenerDatos', obtenerDatos)
       .config(config);


config.$inject = ['$stateProvider', '$urlRouterProvider', 'historial'];

.state('menuestu',{
             url: '/menuestu',
             templateUrl: "Templates/Estudiante/menuestu.html",
             controller: "historialApp"
       })

history.js

angular.module('historialApp', ['ionic'])
   .controller('historial', historial);

historial.$inject = ['$ionicHistory'];

function historial($ionicHistory){
    $ionicHistory.clearHistory();    
}

menu.html - template

<ion-header-bar class="bar-balanced" align-title="center">
<p class="title">Menu Estudiante</p>
</ion-header-bar>

<ion-view>    
    <ion-content has-header="true"> 

    <div class="container" ng-controller="historial">


        </div>

    </div>

</ion-content>

At the moment of injecting the module and trying to call the controller that I added to that module to erase the history, an error is created, and an error is created if it was removed and called from app. js , because when you try to enter the menu view the error comes out and does not progress.

I would like to know what is wrong, if it is the way to inject or call the controller, thank you in advance.

    
asked by Pedro Miguel Pimienta Morales 31.03.2016 в 02:28
source

1 answer

2

You are trying to use a factory or a service in config block . That is a mistake because:

  

A module is a collection of execution and configuration blocks which are applied to the application during the initialization process. in its simplest form a module consists of a collection of two types of blocks:

     
  • Configuration blocks - They are executed in the configuration and registration phase of providers. Only providers and constants can be injected into configuration blocks . This is done to prevent the accidental instantiation of services before they have been fully configured.

  •   
  • Execution blocks - They are executed after the injector is created and used to start the application. Only instances and constants can be injected into execution blocks . This is done to prevent more configurations from being made to the system during the run-time phase.

  •   

    If you look at the highlighted fragments in bold it tells you that only what has been declared with .constant and .provider can be executed in a config block while in the run blocks you can not use what has been declared with .provider

    Simply replace this line in the login.js file

    angular.module('unicesarApp', ['ionic', 'historialApp'])
       .controller('formulario', formulario)
       .service('obtenerDatos', obtenerDatos)
       //.config(config); => quita el config y pon un run
       .run(config)
    

    In the eyes of angular $ionicHistory it is a factory like any other even if it has been declared in a bookstore. Providers are easily identifiable because they end in Provider , such as $ionicConfigProvider .

    Note: I used the notation .run(config) since config is a function that you have declared in your code. If you rename the variable will be less confusing but for angular it does not have relevance since this one only cares about the type of object that you are trying to inject in the block (if it is a factory, a service, etc).

    Update

    When answering the question I thought that historial was a factory that you had tried to add to the module. If this is a controller you are using it incorrectly since the controllers are added to a module automatically when they are declared using the notation

    angular.module('miModulo').controller('miController');
    // ya el módulo "miModulo" contiene un controller llamado "miController"
    // en el ejemplo "miModulo" ya había sido declarado antes
    

    Then when you declare another module and you put dependencies the controllers of one module are added to the other also

    // Se declara un módulo "miApp" y se le especifica que "miModulo" es una dependencia
    angular.module('miApp', ['miModulo']);
    // El controller "miController" se agrega a "miApp" automáticamente
    // o los puedes agregar al módulo "miApp" directamente
    
        
    answered by 31.03.2016 / 18:15
    source