Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Data Oriented Development with Angularjs

You're reading from   Data Oriented Development with Angularjs

Arrow left icon
Product type Paperback
Published in Apr 2015
Publisher
ISBN-13 9781784398057
Length 156 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Manoj Waikar Manoj Waikar
Author Profile Icon Manoj Waikar
Manoj Waikar
Arrow right icon
View More author details
Toc

Difference between a factory and a service


Factories and services are used almost interchangeably in Angular, however there is a subtle but important difference between the two. So let's see their usage and it'll be amply clear when and how to use each one. First let's use the service command:

app.service('myService', function(){
    this.hello = function() {
        return "Hello World";
    };
});

(Chapter6\example-app\fact-svc\my.svc.js)

myService has a single function called hello which returns "Hello World" when called. Now let's use the factory command:

app.factory('myFactory', function(){
    return {
        hello: function() {
            return "Hello World";
        }
   
	}
});

(Chapter6\example-app\fact-svc\my.fctry.js)

myFactory has a single function called hello which returns "Hello World" when called as well. So what's the difference? For that, let's look at a factory which can accept or maintain some state as shown in the following code:

app.factory('myFactoryWithState', function...
lock icon The rest of the chapter is locked
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Data Oriented Development with Angularjs
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Modal Close icon
Modal Close icon