angularjsdependency-injectionangularjs-module

Another way to inject in Angular


I have a template and I'm changing the way to Inject dipendecies. It has the 'classic' way to inject. I want to replace it with the compact form. I can't do these line of code. I've tried but I can't understand the struscture. Because I am accustomed in the other form. Can somebody help me?

(function() {
'use strict';

angular.module('app.data')
  .factory('postResource', postResource)
  .factory('postsUtils', postsUtils);

postResource.$inject = ['$resource'];

function postResource($resource) {
  return $resource('/api/posts/:id', {id: '@id'}, {
    update: {
      method: 'PUT'
    }
  });
}

postsUtils.$inject = ['postResource'];
function postsUtils(postResource) {
  function postsDuringInterval(posts, days) {
    var today = new Date();
    var interval = 86400000 * days;
    var postsDuringInterval = [];
    posts.forEach(function(post) {
      var postDate = new Date(post.date);
      today - postDate < interval && postsDuringInterval.push(post);
    });
    return postsDuringInterval;
  }

  function recent(posts, postsNum) {
    posts.sort(function(a, b) {
      if (a.date < b.date) return 1;
      else if (a.date == b.date) return 0;
      else return -1;
    });
    return posts.slice(0, postsNum || 1);
  }

  function lastEdited(posts) {
    var lastEdited = posts[0];
    posts.forEach(function(post) {
      lastEdited = lastEdited.date < post.date ? lastEdited : post;
    });
    return lastEdited;
  }

  return {
    postsDuringInterval: postsDuringInterval,
    lastEdited: lastEdited,
    recent: recent
  }
}
})();

Solution

  • Here is an example of how you would inject dependencies.

    var app = angular.module('myApp',
        ['ngRoute', 'ngSanitize', 'ui.bootstrap', 'angular-flexslider',
            'ng-backstretch', 'angular-parallax', 'fitVids', 'wu.masonry', 'timer',
            'uiGmapgoogle-maps', 'ngProgress']);
    

    An example of a controller that has a service injected into it.

        app.controller('ContactController',['$scope','contactService', 
        function($scope, contactService) {
    
     var self = this;
        self.contact = {id:null, name:"",lastName:"",email:"",subject:"",message:""};
    
        this.submit = function(){
            contactService.submit(self.contact);
            self.contact = {id:null, name:"",lastName:"",email:"",subject:"",message:""};
        };
    }]);
    

    The factory:

    app.factory('contactService',['$http','$q', function($http,$q){
    
        return {
    
            submit: function (contact) {
                return $http.post('/sendForm/', contact)
                    .then(
                        function (response) {
                            return response;
                        },
                        function (errResponse) {
                            console.error("Error while submitting form" + errResponse);
                            return $q.reject(errResponse);
                        }
                    )
            }
    
        }
    }]);
    

    I think this is the way you were referring too. Hope this helps.