Skip to content

Instantly share code, notes, and snippets.

@kneerunjun
Last active November 26, 2015 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kneerunjun/c3f0038b32f6f4e2a3b3 to your computer and use it in GitHub Desktop.
Save kneerunjun/c3f0038b32f6f4e2a3b3 to your computer and use it in GitHub Desktop.

Your controllers have manifestation but not the services

<body ng-app="myApp" ng-controller="mainController">
<!-- here you go the scope has the existence since the controller has manifestation -->
</body>
var myApp  = angular.module("myApp", []);
var mainController = myApp.controller("mainController", function($scope, myService){
  //scope has a purpose and that is to link the object model to the html there in the view
  myService.getFromServer($scope.id).then(function(){
    //handle the object that is thrown by resolve case
  }, function(){
    //handle the case that is thrown by reject case
  });
})
var myService = myApp.service("myService", function($http, $q){
  this.getFromServer = function(id){
    var deferred = $q.defer();
    $http.get(url).then(function(){
      //success of the server API call
      // HTTP 200 OK 
      deferred.resolve({});
    }, function(){
      //failure of the server API call
      deferred.reject({ErrorMessage:"your error message from the server"})
    })
    return deferred.promise
  }
})
  • Services have no manifestation they are just to contain some logic that is agnostic of the view / html but reusable across the controllers
  • Controllers can have $scope and that has attributes which in turn are manifested
  • While making API calls it is an Aysnc operation and so it would involve promises
  • Promises are simple and have resolution and rejection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment