From 2bc1c9f26110882a379b62df0f4ec129ebcfecd1 Mon Sep 17 00:00:00 2001 From: Steve Smith Date: Mon, 4 May 2015 12:19:17 -0400 Subject: [PATCH] Migrated up to Services. --- docs/client-side/angular.rst | 18 ++++++++++++++++++ .../sample/src/wwwroot/app/personFactory.js | 11 +++++++++++ 2 files changed, 29 insertions(+) diff --git a/docs/client-side/angular.rst b/docs/client-side/angular.rst index 3b7b2f7324..79176afc68 100644 --- a/docs/client-side/angular.rst +++ b/docs/client-side/angular.rst @@ -276,7 +276,25 @@ The page shows "Mary" and "Jane" that correspond to the ``firstName`` and ``last Services ^^^^^^^^ +Services in AngularJS are commonly used shared code that are abstracted away into a file that can be used throughout the lifetime of an angular application. Services are lazily instantiated, meaning that there will not be an instance of a service unless a component that depends on the service gets used. Factories are an example of a service used in AngularJS applications. Factories are created using the ``myApp.factory()`` function call, where ``myApp`` is the module. +Below is an example that shows how to use factories in AngularJS: + +.. literalinclude:: angular/sample/src/wwwroot/app/personFactory.js + :language: javascript + :linenos: + :emphasize-lines: 1 + +To call this factory from the controller, pass ``personFactory`` as a parameter to the ``controller()`` function: + +.. code-block:: javascript + + personApp.controller('personController', function($scope,personFactory) { + $scope.name = personFactory.getName(); + }); + +Using services to talk to a REST endpoint +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/docs/client-side/angular/sample/src/wwwroot/app/personFactory.js b/docs/client-side/angular/sample/src/wwwroot/app/personFactory.js index e69de29bb2..1f9356db63 100644 --- a/docs/client-side/angular/sample/src/wwwroot/app/personFactory.js +++ b/docs/client-side/angular/sample/src/wwwroot/app/personFactory.js @@ -0,0 +1,11 @@ +personApp.factory('personFactory', function ($http) { + function getName() { + return "Mary Jane"; + } + + var service = { + getName: getName + }; + + return service; +}); \ No newline at end of file