Posted inTechnology / AngularJS

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 Pakainfo.com – 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

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 ,
 function(data)
 {
//Getting data from myServicefunction(factory)
 $scope.datalist = data ;// response from service
 },
 function( data)
 {
  //error
 });
};
});

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

  
  

[/php

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

 

Example of Factory File in angular

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

  success(data);

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

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

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(Pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype