Oct 21, 2017

Textual description of firstImageUrl

Factory and Decorator in AngularJS

Factory pattern is very ubiquitous design pattern which finds its uses in most of the design strategy. In this post we will use factory with Decorator and demonstrate - how decorator can be used along side of factory and provide extra functionality without disturbing cleanness of factory.

We will take user input and in return displays reversed output of that input string. Factory comes into picture to give an instance which contains getter and setter method. We define decorator for this Factory instance and reverse input string set via setter method.

Script for Factory and Decorator:


'use strict'

var app = angular.module('DecoratorApp', []);

app.controller("decoratorController", function($scope, reverseFactory) {
 $scope.reverseCall = function() {
  reverseFactory.setData($scope.inputVal);
  reverseFactory.reverse();
  $scope.outputVal = reverseFactory.getData();
 }
});

app.factory('reverseFactory', function() {
 var myPrivateData;
 return {
  getData : function() {
   return myPrivateData
  },
  setData : function(data) {
   return myPrivateData = data
  }
 }
})

/* Decoration created for factory reverseFactory */
app.config(function($provide) {
 $provide.decorator('reverseFactory', function($delegate) {
  $delegate.reverse = function() {
   $delegate.setData($delegate.getData().split('')
     .reverse().join(''))
  }
  return $delegate
 })
})

HTML for input and output  :
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Filter application</title>
</head>
<body ng-app="DecoratorApp" ng-controller="decoratorController" ng-cloak>
 <div class="mainBody">
  <div class="mainBodyMinHeight">
   <img ng-src="https://toddmotto.com/img/tags/angularjs.svg"
    height="100px">
   <p>
    <label>Input String: </label>
   </p>
   <div flex="100">
    <input type='text' ng-model="inputVal" ng-blur="reverseCall()" />
   </div>
   <hr />
   Reversed String:
   <div flex="100">
    <p>{{outputVal}}</p>
   </div>
  </div>
 </div>
 <script
  src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/
 1.4.3/angular.min.js"></script>
 <script src="scripts/decoratorApp.js"></script>
</body>
</html>

Output:-



Download complete source code.
Location: Bengaluru, Karnataka, India

8 comments:


  1. These are really amazing and valuable websites you have shared with us. Thanks for the informative post. Keep posting like this information.
    hr solutions in Chennai | hr consultancy in Chennai | hr recruiter in chennai

    ReplyDelete



  2. Our Russian Escorts in Mahipalpur have smooth bodies that can prepare you for a night of sexual loving. You don’t want to feel self-conscious as you’re safe here at our Call Girls agency. Our Services are all-time service that is accessible for you 24*7 hours. Our Escorts Girls are always prepared to make you fill you’ll have a fantastic alluring night with our lovely girls and you only need for believing simultaneously. Check our other Services also...
    Call Girl in Chanakyapuri
    Independent Call Girls in Ahmedabad
    Escorts in Agra
    Call Girls in Pune

    ReplyDelete


  3. We have been in the industry for several years now and have been able to satisfy hundreds of clients who have come back to us wanting more. Our Escorts Service in Agra to book so that their needs may be fulfilled. We establish a detailed conversation with our clients and help them choose the best amongst the various choices available.
    Female Escorts in Agra
    Russian Escorts in Agra
    Female Escorts in Faridabad
    Call Girls in Faridabad

    ReplyDelete




  4. Also known to come with crucial offerings for its sensible, down-to-earth and important clients,Call Girls in Gurgaon really means in fulfilling their lives with cheerful moments to make love in concern to ensure them live their lives in abetter way. Check our other Services...
    Charges of Call Girls in Gurgaon
    Escorts Service in Gurgaon
    Female Escorts in Gurgaon
    Gurgaon Escorts Service
    Housewife Escorts in Gurgaon



    ReplyDelete