Inject factory in controller use strict and named , IIEF functions

i'm using JHispter and i saw that uses these AngularJS rules: https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md

Using IIFE,Getters,Use Strict, Named Functions,ControllerAs,etc i would like to create a simple page that parse a JSON and show a movie list (title, director,duration) and the one that lasts much longer. I've searched and tried all day but nothing works. The factory can't be used in the controller tough i inject it using $inject.

That's my index.html

<html>
    <head>
        <title>Hello Angular</title>
        <link href="stile.css" rel="stylesheet" type="text/css">
        <link rel="shortcut icon" href="">
    </head>
    <body ng-app="myApp">
        <h1>Hello Angular</h1>
        <div ng-controller="myController as sc">
            <h1>angular JSON test</h1>
           <!--  <p>Print movie list</p>
            <ul >
                <li ng-repeat="film in sc.elencoFilm">
                    {{film.title}}, {{film.director}}, {{film.time}}
                </li>
            </ul>

            <p >Trova il film più lungo: {{sc.maxTimeFilm().title}} </p> -->
        </div>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
        <script src="JS/app.config.js"></script> 
        <script src="JS/app.state.js"></script> 
        <script src="JS/app.service.js"></script>
        <script src="JS/app.controller.js"></script> 
    </body>
</html>

My app.config.js

(function() {
    'use strict';

    angular
    .module("myApp", []) ;
})();

My app.state.js

(function() {
    'use strict';

    angular
        .module('myApp')
        .config(stateConfig);

    stateConfig.$inject = ['$routeProvider'];

    function stateConfig($routeProvider) {
        $routeProvider.when('/', {
      templateUrl:"index.html",
      controller:"serverController"
    });
    }
})();

My app.controller.js

(function () {
    'use strict';
    angular
            .module("myApp",[])
            .controller("myController", myController);


    //myController.$inject = ['$scope', '$http'];
    myController.$inject = ['$scope', '$http','myFactory']; 

    function myController($scope, $http, myFactory) {
    //function myController($scope, $http){//, myFactory) {
        var vm = this;
        var elencoFilm={};
        myFactory.getMovies().then(function (response) {
            vm.elencoFilm = response;
        });

        vm.maxTimeFilm = getMaxTimeFilm();
        function getMaxTimeFilm() { //return the longest film
        }
    }    
})();

My app.service.js

(function () {
    'use strict';
    angular.module('myApp',[])
            .factory('myFactory', myFactory);

    myFactory.$inject = ['$scope', '$http','myFactory'];
    function myFactory($scope, $http) {
        console.log("sono nella factory");
        return {
            getMovies: function ($http) {
                return $http.get('https://api.myjson.com/bins/1bgtg3');
                       /* .then(function (response) {
                            return response.data.movies;
                        });*/
            }
        }
    }

})();

it always return this error: https://docs.angularjs.org/error/$injector/unpr?p0=myFactoryProvider%20%3C-%20myFactory%20%3C-%20myController

it can't recognize myFactory into myController function!

in app.controller.js this line

function myController($scope, $http, myFactory) { this break out the error!

Thank you for the help!! :)

Answers:

Answer

Do not add empty dependency array in for module myApp in controller and factory. Use .module('myApp') in both controller and factory, similar to your config.

Answer

By defining your modules based on functionality the myFactory service should be under a encapsulated closure referencing the main app module, hence all your factories can go under this module (ex. factories.module.js) :

 (function() {
     'use strict'

     angular.module('myApp.factories', []);

 }();

Once that module is added to your app.config

(function() {
   'use strict'

   angular.module('myApp', [
    'myApp.factories'])

})();

It separates the concerns of your modules based on functionality following the IIFE design principle. Now reference your new module to myFactory in your service file.

(function () {
  'use strict'

  angular.module('myApp.factories')
         .factory('myFactory', myFactory)

  ...
Answer

I've solved it! i simply removed $scope from factory and removed [] in the controller definition (.module("myApp") insted of .module("myApp",[]) ). @23rharris your advice is a best practice?

I've used the factory in each function of the controller every time i needed the JSON file :

myController.$inject = ['$scope', 'myFactory'];
    function myController($scope, myFactory) {
...
 vm.elencoFilm = getMovies();
        function getMovies() {
            myFactory.getMovies().then(function (response) {
...
...
   vm.maxTimeFilm =getMaxTimeFilm();
        function getMaxTimeFilm() {
 myFactory.getMovies().then(function (response) {
                if (response.data.movies != undefined) {
...

Is this the correct pattern for REST?

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.