82431

Inject service in all controllers

Question:

I want to use <a href="https://github.com/alexcrack/angular-ui-notification" rel="nofollow">https://github.com/alexcrack/angular-ui-notification</a> for notifications. I need them in all my controllers. Is it possible to inject 'Notification'(or '$log' or whatever) in all my controllers?

Answer1:

I think you could by letting your controllers inherit from a common basecontroller. Something like this might work:

angular.module('extending', []) .controller('baseController', function(someService) { this.someService = someService; }) .controller('extendedController', function($scope, $controller) { angular.extend(this, $controller('baseController', { $scope: $scope })); this.alert = this.someService.alert; }) .service('someService', function() { this.alert = function() { window.alert('alert some service'); }; });

HTML

<body> <div ng-controller="extendedController as ex"> <button ng-click="ex.alert()">Alert</button> </div> </body>

Example on <a href="http://plnkr.co/edit/KDdNgHVQ782nxRS7LR6w?p=preview" rel="nofollow">plunker</a>. <a href="https://stackoverflow.com/questions/16539999/angular-extending-controller" rel="nofollow">Related</a> post on SO. AngularjS <a href="https://docs.angularjs.org/api/ng/function/angular.extend" rel="nofollow">extend</a> doc.

Recommend

  • jQuery click funtion executes only once
  • Quickly finding project files using Emacs EDE
  • Importing OBJ file to maya scene (MEL/Python)
  • Symfony: Override FOSUserBundle Resetting Controller
  • Laravel redirect to post method
  • T-SQL function to generate calendar table
  • Remote computer drive information is not precise
  • PayPal -error 400 for US and Canada addresses
  • load store for List using json
  • How to use .jar in a pig file
  • Ajax Get XML - Google Directions
  • django internationalisation works locally but not on aws elastic beanstalk
  • How can I override the Authorize attribute so I can put a breakpoint on it
  • Confused about strong parameters with Devise
  • Selecting an option based on the value of its data attribute
  • How to validate a Datetime value in javascript?
  • Laravel 5.1 - Error storage form data,
  • Regex Java word context
  • Overriding Default FOSUserBundle Controller in Symfony 3.4
  • Retrieve AVG Temp by Month using BigQuery?
  • ASP.NET MVC - Custom IIdentity or IPrincipal with Windows Authentication
  • KoGrid how to refresh grid in case data is changed
  • PHP/MySql Multiple Dropdowns dont want option values visible in page source
  • Laravel 4.2 - ReflectionException (-1)
  • php printf adds number to output
  • why does sort with uniq not work together
  • Display custom checkout field value in Woocommerce admin order edit pages
  • SurfaceView or GLSurfaceview?
  • ASP.NET MVC : How to create own HttpContext
  • Best way with relation tables
  • Laravel routing: how to pass parameter/arguments from one controller to controller
  • PHP - Use database in MVC view layer (Laravel Blade)
  • Microdata and multiple opening in a day
  • Fatal error: Using $this when not in object context explanation?
  • JSON to Objects in java?
  • How to access a value using a space-separated key?
  • Centering a Tkinter Toplevel window in both windows and remote X11?
  • How to create a random time within a range
  • Create a demo of my static program analyzer as a web application? [without sending .jar files to cli
  • Best way to check whether a TextBox is empty or not
  • Stop a background process in flask without creating zombie processes
  • Translating route segments with ZF's gettext adapter
  • Android: Button background XML sometimes loses alpha when setting color filter
  • stuck with encodings in python with BeautifulSoup
  • Use MongoDB array as stack
  • Setting inner div opacity to 1, but not effected
  • Dropdown menu with the dropdown-menu-right class does not align to the right
  • Looking for datastructure that maintains a size & purges older elements in the process
  • How will Roslyn help me in avoiding a recompile to deploy changes to my ASP.NET website?
  • How to upload file on another domain?
  • Ant: fileset “dir” attribute with a runtime expanded full path
  • Fixed Background Works in Chrome but Not Firefox?
  • SPARQL date range
  • Less Conflicting Session Manager for Zope 2
  • Change Checkbox value without raising event
  • ThreadStatic in asynchronous ASP.NET Web API
  • ASP.NET MVC Application won't update some controllers
  • CakePHP ACL tutorial initDB function warnings
  • Does Mobilefirst provide a provision to access web services directly?
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • Different response to non-authenticated users and AJAX calls
  • Do create extension work in single-user mode in postgres?
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • bootstrap to use multiple ng-app
  • Comma separated Values
  • Running Map reduces the dimensions of the matrices
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?