Book Image

AngularJS Web application development Cookbook

By : Matthew Frisbie
Book Image

AngularJS Web application development Cookbook

By: Matthew Frisbie

Overview of this book

Packed with easy-to-follow recipes, this practical guide will show you how to unleash the full might of the AngularJS framework. Skip straight to practical solutions and quick, functional answers to your problems without hand-holding or slogging through the basics. Avoid antipatterns and pitfalls, and squeeze the maximum amount out of the most powerful parts of the framework, from creating promise-driven applications to building an extensible event bus. Throughout, take advantage of a clear problem-solving approach that offers code samples and explanations of components you should be using in your production applications.
Table of Contents (17 chapters)
AngularJS Web Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using service values and constants


AngularJS service types, at their core, are singleton containers used for unified resource access across your application. Sometimes, the resource access will just be a single JS object. For this, AngularJS offers service values and service constants.

How to do it…

Service values and service constants both act in a very similar way, but with one important difference.

Service value

The service value is the simplest of all service types. The value service acts as a key-value pair and can be injected and used as follows:

(app.js)

angular.module('myApp', [])
.controller('Ctrl', function($scope, MyValue) {
  $scope.data = MyValue;
  $scope.update = function() {
    MyValue.name = 'Brandon Marshall';
  };
})
.value('MyValue', {
  name: 'Tim Tebow',
  number: 15
});

An example of template use is as follows:

(index.html)

<div ng-app="myApp">
  <div ng-controller="Ctrl">
    <button ng-click="update()">Update</button>
    {{ data.name }} #{{...