Book Image

AngularJS UI Development

By : Amit Gharat, Matthias Nehlsen
Book Image

AngularJS UI Development

By: Amit Gharat, Matthias Nehlsen

Overview of this book

<p>AngularJS and its rich set of components solve many of the problems developers face when writing reliable single page applications in ways that would not be possible using other frameworks. This book will help you expand your horizons by teaching you the skills needed to successfully design, customize, build, and deliver real-world applications in AngularJS. We will start off by setting up a fully automated environment to quickly scaffold, test, and deploy any application. Along the way, we'll cover how to design and build production-ready applications to demonstrate how innovative and powerful AngularJS is. By leveraging CSS3 animations, we'll convert them into intuitive and native-like applications in no time. You will also learn how to use Grunt for application-specific task management, Bower to manage dependencies with external libraries/plugins, Git for better versioning, and Karma and Protractor for automated testing to build applications the way experts do.</p> <p>You will learn all this by building real-world applications including a to-do application, Github dashboard, project management application, and many more.</p>
Table of Contents (17 chapters)
AngularJS UI Development
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a service in AngularJS


We have previously discussed that a controller might not be a good place to hold on to data. If the controller goes out of the scope, the data residing in the controller will become inaccessible, and garbage will get collected in the JavaScript virtual machine.

Services are singletons, on the other hand. They get instantiated once and stay alive during the entire life cycle of the application. Let's create such a service.

We will make this service known to the application by editing app.js as follows:

'use strict';

angular.module('myApp', ['myApp.controllers', 'myApp.filters', 'myApp.services', 'ng-grid']);

Then, we create a file named services.js that contains the following code:

'use strict';

/** data service, manages data model throughout the life cycle of the application */
angular.module('myApp.services', []).factory('dataService', function () {
    var exports = {};

    exports.data = ["x", "y"];

    return exports;
});

In this service module, we use the...