Angular 4: Building a Simple Service

Create the project with angular-cli $ ng new angular-service-project Create a service with angular-cli The service was created with, $ ng g s example-service where ‘g’ is a shortcut for ‘generate’ and ‘s’ is a shortcut for ‘service’. The actual service is at: example-service-service.ts import { Injectable } from ‘@angular/core’; @Injectable() export class ExampleServiceService {…Read More

Angular Service Example

The following code demonstrates how a service can be created in AngularJS. index.html <html> <head> <script src=”https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js”></script> <script src=”https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js”></script> <script src=”https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-route.min.js”></script> <script src=”https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-animate.min.js”></script> <script src=”app.js”></script> <script src=”anotherModule.js”></script> </head> <body> <div ng-app=”mainModule” ng-controller=”mainController as alias”> <b>Invoice:</b> <p>The input is: {{alias.quantity}}</p> <div> Quantity: <input type=”number” min=”0″ ng-model=”alias.quantity”required> </div> <div><p>Add 4 and double it!</p></div> <div> <b>Result:</b> <span>{{alias.total(alias.quantity)}}</span> <button…Read More