Six stars of AngularJS - Part 1

       // provider sample 1
       $provide.provider('movieService', movieServiceProvider);

            // provider
            function movieServiceProvider() {
            var self = this;
            this.$get = movieService; // factory
            this.isFrench = false;
        
            function movieService() {
                return { // service
                    getAllMovies: getAllMovies
                };

                function getAllMovies() {

                    if (self.isFrench) {
                        return ['frenchMovie1', 'frenchMovie2', 'frenchMovie3'];
                    } else {
                        return ['engMovie1', 'engMovie2', 'engMovie3'];
                    }
                }

            }
        }
    // provider sample 2
    var app = angular.module('app', []);

    app.config(function($provide) {

        // creates and register's provider
        var movieServiceProviderInstance = $provide.provider('movieService',  movieServiceProvider);

        // configures provider
        movieServiceProviderInstance.isFrench = true;

        function movieServiceProvider() {
            var self = this;
            this.$get = movieService;
            this.isFrench = false;

            function movieService() {
                return {
                    getAllMovies: getAllMovies
                };

                function getAllMovies() {

                    if (self.isFrench) {
                        return ['frenchMovie1', 'frenchMovie2', 'frenchMovie3'];
                    } else {
                        return ['engMovie1', 'engMovie2', 'engMovie3'];
                    }
                }

            }
        }

    });
    // Sample 3
    var app = angular.module('app');
    app.provider('movieService', movieServiceProvider);

    function movieServiceProvider() {
        var self = this;
        this.$get = movieService;
        this.isFrench = false;

        function movieService() {
            return {
                getAllMovies: getAllMovies
            };

            function getAllMovies() {

                if (self.isFrench) {
                    return ['frenchMovie1', 'frenchMovie2', 'frenchMovie3'];
                } else {
                    return ['engMovie1', 'engMovie2', 'engMovie3'];
                }
            }

        }
    }
comments powered by Disqus