Code Monkey home page Code Monkey logo

angular-what-is-a-custom-service-readme's Introduction

What is a Service?

Overview

We've used Angular's built-in services already, but wouldn't it be cool if we could create our own? We can!

Objectives

  • Describe custom Services
  • Create a custom Service
  • Bind the Service to the module
  • Inject the Service into Controller
  • Use custom Service

Custom Services

Angular allows us, much like controllers, to create custom services. These are very powerful and can do a lot for us, such as communicating with APIs or manipulating data. We can inject these into any controller we'd like to, meaning we won't be repeating ourselves.

This fits perfectly into our "MVVM" pattern. We can create services (our helpers) to do all the "dirty" work for us (communicate with APIs, etc), and we can then utilise them in our controllers. This keeps our controllers thin and all the business logic inside our services.

.service()

Our services follow the same setup as our custom controllers. We use .service to create them! This is the basic setup of a service:

function SomeService() {
	this.someFunction = function () {

	};
}

angular
	.module('app')
	.service('SomeService', SomeService);

Very easy! With services, much like our controllers (when using controllerAs), we attach all of our functions to this. These will be publicly accessible by anyone who injects the service. We can also create private functions by using normal functions, as such:

function SomeService() {
	function privateMethod() {

	}

	this.publicMethod = function () {
		privateMethod();
	};
}

angular
	.module('app')
	.service('SomeService', SomeService);

Injecting our services

Much like when we used $timeout, we can inject our custom services into our controllers by using their name. To inject our SomeService above, we just add SomeService as an argument to our controller:

function SomeController(SomeService) {
	SomeService.publicMethod();
}

angular
	.module('app')
	.controller('SomeController', SomeController);

Simple!

View What Is A Custom Service on Learn.co and start learning to code for free.

angular-what-is-a-custom-service-readme's People

Contributors

annjohn avatar imkaruna avatar ipc103 avatar toddmotto avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.