Skip to content

Instantly share code, notes, and snippets.

@akotlov
Forked from Mithrandir0x/gist:3639232
Created May 27, 2014 00:22
Show Gist options
  • Save akotlov/f60532bdd5bb516b2544 to your computer and use it in GitHub Desktop.
Save akotlov/f60532bdd5bb516b2544 to your computer and use it in GitHub Desktop.

Revisions

  1. @Mithrandir0x Mithrandir0x revised this gist Sep 9, 2012. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,9 @@ myApp.factory('helloWorldFromFactory', function() {

    //provider style, full blown, configurable version
    myApp.provider('helloWorld', function() {
    // In the provider function, you cannot inject any
    // service or factory. This can only be done at the
    // "$get" method.

    this.name = 'Default';

  2. @Mithrandir0x Mithrandir0x created this gist Sep 5, 2012.
    54 changes: 54 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    // Source: https://groups.google.com/forum/#!topic/angular/hVrkvaHGOfc
    // jsFiddle: http://jsfiddle.net/pkozlowski_opensource/PxdSP/14/
    // author: Pawel Kozlowski

    var myApp = angular.module('myApp', []);

    //service style, probably the simplest one
    myApp.service('helloWorldFromService', function() {
    this.sayHello = function() {
    return "Hello, World!"
    };
    });

    //factory style, more involved but more sophisticated
    myApp.factory('helloWorldFromFactory', function() {
    return {
    sayHello: function() {
    return "Hello, World!"
    }
    };
    });

    //provider style, full blown, configurable version
    myApp.provider('helloWorld', function() {

    this.name = 'Default';

    this.$get = function() {
    var name = this.name;
    return {
    sayHello: function() {
    return "Hello, " + name + "!"
    }
    }
    };

    this.setName = function(name) {
    this.name = name;
    };
    });

    //hey, we can configure a provider!
    myApp.config(function(helloWorldProvider){
    helloWorldProvider.setName('World');
    });


    function MyCtrl($scope, helloWorld, helloWorldFromFactory, helloWorldFromService) {

    $scope.hellos = [
    helloWorld.sayHello(),
    helloWorldFromFactory.sayHello(),
    helloWorldFromService.sayHello()];
    }