Passing current scope to an AngularJS Service


       


var app = angular.module('as2App');

app.factory('AlertService', function($timeout) {

  var _scope = {};

  //constructor

  function alertService(scope) {

    this._scope = scope;
    this._scope['show_alert'] = false;
    this._scope['alerts'] = [];
  }


  alertService.prototype._addAlert = function(type, message) {
    this._scope['alerts'].push({
      type: type,
      msg: message
    });
    this._onShow(this._scope);
  }



  alertService.prototype._onShow = function(_scope) {
    this._scope['show_alert'] = true;
    $timeout(function() {
      alertService.prototype._hideMe(_scope);
    }, 2000);
  }


  alertService.prototype._hideMe = function(_scope) {
    _scope['show_alert'] = false;
    _scope['alerts'].splice(0, 1);
  }


  return alertService;

});



//########### SERVICES ##############


//########### CNTROLLER  ##############



var app = angular.module('as2App');

app.controller('AdvCtrl',

function($scope, AlertService) {
     var alertService = $scope.AlertService = new AlertService($scope);
     $scope.delete = function(advertiser) {
     alertService._addAlert(msgType.success, " Record deleted successfly.Deleted ID = " + advertiser._id);
}



https://gist.github.com/muratatak77/bbe743e379add276cf59cc1fc90b7efb

Comments

Popular posts from this blog

Design a notification system

NETFLIX HIGH-LEVEL SYSTEM DESIGN

URL Shortener System Design