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

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

[/php
<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>

[php]
 <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 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.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Vuejs Live Search Filter Example with Demo
  2.   Factory Service using Angular Example
  3.   5 Star Product Rating Review Widget in CSS
  4.   vuejs Radio Button on Form Submit Example
  5.   PHP Upload and Crop Image using jQuery Ajax
  6.   AngularJS - update bind ng-model input value from Jquery Code
  7.   Fresher PHP WEB Developer Salary Range Level
  8.   AngularJS HTML DOM Manipulation Example
  9.   Vue JS Lightweight Trumbowyg WYSIWYG editor component
  10.   Data Binding using Angular Example

Leave a Reply

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