how to call factory method from controller in angularjs

how to call factory method from controller in angularjs

In this Post We Will Explain About is how to call factory method from controller in angularjs With Example and Demo.Welcome on – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to angularjs factory controller example Example

In this post we will show you Best way to implement angularjs call controller function from factory, hear for AngularJS Tutorial: Creating a new service using Factory with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Communication between factory file and controller in Angular js

angular Controller is a simple JavaScript constructor some function that is used to argument or paramaters the Angular scope. This is some communicating with HTML views and some factory data files as well as with angular directives ,angular filter etc .here learn to Factory files are some data interacting with angular with backend services through PHP RESTful API and usually client side to server side communicating with data or specific controls

Read Also:  Laravel 5.7 Get Current Page URL path

Controller example

Here in this example, liveCtrl is the angular controller name and LiveSimpleFactoryServiceList here is the factory simple file name

angular.module('liveApp').controller( 'liveCtrl',
function ($rootScope, $scope ,LiveSimpleFactoryServiceList )
$scope.students = {
 value : '',
 data : '' 
$scope.myformSubmit = function()
//Sending data ($scope.students ) to myServicefunction (factory)
 LiveSimpleFactoryServiceList.myServicefunction( $scope.students ,
//Getting data from myServicefunction(factory)
 $scope.datalist = data ;// response from service
 function( data)

Here show to $scope.students object is nothig but we can take some value from HTML DOM view .For in this example in HTML view We have a DOM input box

Read Also:  Laravel 6 JQuery Ajax Post Request Example

 <input ng-model="students.value"> 
 <input ng-model=""> 

<p>We can have this some values in angular controller by using object of the <b>$scope.students</b> . We can initialize simple myformSubmit function on angular simple ngclick events or by HTML form submission .</p>

 <button ng-click="myformSubmit();"> Submit </button>

Example of Factory File in angular

'use strict';
angular.module('App').factory( 'samplefactoryservice',
function( $rootScope, $http)
return {
//Devloped by  data($scope.students) from controller
myServicefunction : function( data  )
 var data = {
   value:data.value ,
 var url = '';
 //Devloped by  Sending to service ( backend )
 $ url, angular.toJson(data) )
 .success( function( data )
//Devloped by Getting data from backend


 //live data Send back to angular controller
 .error( function( data)
//Live yourotherfunction : etc .....

You are Most welcome in my youtube Channel Please shubscibe my channel. and give me feedBackMore Details……
Angularjs Example

Read Also:  Factory Service using Angular Example


I hope you have Got What is how to call factory method from controller in angularjs And how it works.I would Like to have FeaeBack From My Blog( readers.Your Valuable FeedBack,Any Question,or any Comments abaout This Article( Are Most Always Welcome.