Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I noticed something interesting when playing around with providers.</p> <p>Visibility of injectables is different for providers than it is for services and factories. If you declare an AngularJS "constant" (for example, <code>myApp.constant('a', 'Robert');</code>), you can inject it into services, factories, and providers.</p> <p>But if you declare an AngularJS "value" (for example., <code>myApp.value('b', {name: 'Jones'});</code>), you can inject it into services and factories, but NOT into the provider-creating function. You can, however, inject it into the <code>$get</code> function that you define for your provider. This is mentioned in the AngularJS documentation, but it's easy to miss. You can find it on the %provide page in the sections on the value and constant methods.</p> <p><a href="http://jsfiddle.net/R2Frv/1/" rel="noreferrer">http://jsfiddle.net/R2Frv/1/</a></p> <pre><code>&lt;div ng-app="MyAppName"&gt; &lt;div ng-controller="MyCtrl"&gt; &lt;p&gt;from Service: {{servGreet}}&lt;/p&gt; &lt;p&gt;from Provider: {{provGreet}}&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;script&gt; var myApp = angular.module('MyAppName', []); myApp.constant('a', 'Robert'); myApp.value('b', {name: 'Jones'}); myApp.service('greetService', function(a,b) { this.greeter = 'Hi there, ' + a + ' ' + b.name; }); myApp.provider('greetProvider', function(a) { this.firstName = a; this.$get = function(b) { this.lastName = b.name; this.fullName = this.firstName + ' ' + this.lastName; return this; }; }); function MyCtrl($scope, greetService, greetProvider) { $scope.servGreet = greetService.greeter; $scope.provGreet = greetProvider.fullName; } &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload