0

angular.module('myFirstApp')// 
//begin controller
.controller('myController',function($scope,$http,personFactory){ //hi im controller i need to call the http service from the factory 
  
  //begin service
  //hi im service i dont want to live in controller.js , i want to live in app.js
  //can we put the service inside a factory which is in app.js file and get called using controller #1
   $http.get('iphone.json').success(function(result){
    $scope.ObjectArray = result;
    
  }).error(function(error){
    alert(error.error + "/" + error.statusCode);
  }); //end
 
  
  //begin
  // hi i am controller #1 , i live in controller.js , i need to call http service in factory and send the value to HTML
   $scope.retrieveRecords = function(){
    var x = personFactory.getData();
    return x 
  }//end
  
  
  // i am controller #2
  $scope.addRecord = function(){
    var x = personFactory.insertData($scope.Name,$scope.Email,$scope.Password,$scope.Mobile,$scope.result);
    $scope.message = "You have successfuly added new data";
    return x + ' ' + $scope.message;
  }
  
  // i am controller #3
  $scope.editRecord = function(){
    var x = personFactory.updateData();
    $scope.message = "You have successfuly updated the data";
  return x + ' ' + $scope.message;
  }


})

//end controller

//begin factory
.factory('personFactory',function(){ //hi im factory i live in app.js , im waiting for http service to live here
  //end factory

what i need so much , please answer with something clever, all i need is the service separated to controller , what else should i say why is not posting damn blalbalblbllbalbalblablalkjvlwakjvnklwavjnlkwanvwanviwbilawvwavawvas vasvklwanvljabwjkabwv

1
  • viewData? Also notice that the $http request returns a Promise so you may need to assign the value to $scope.viewData differently like: personFactory.getData().then(function(data) { $scope.viewData = data}); Commented Nov 18, 2016 at 8:49

2 Answers 2

1

return {getData} is not right syntax in service

angular.module('myFirstApp',[])
.factory('personFactory',function(){

    function getData(){       
      var x = $http.get('iphone.json').success(function(result){
      $scope.items = result;
      alert(result);
      }).error(function(error){
      alert(error.error + "/" + error.statusCode);
      })    
      return x;
    }
  return {
      getData: getData
   };
Sign up to request clarification or add additional context in comments.

Comments

0

Change your factory from angular.module('myFirstApp',[]).factory to angular.module('myFirstApp').factory i.e., use angular.module('myFirstApp') but not angular.module('myFirstApp', [])

6 Comments

sorry , i have a ngRoute in that so i cannot exclude it, ill modify my code to include routing
When you use angular.module('myFirstApp',[]), it is not a getter of the existing module rather it creates new. Just remove [] from your factory js file. It will work
plnkr.co/edit/w53VjZDFnuJnE7itp8vR?p=preview , can you show me a nice way of coding
In your plunker, you have defined factory in app.js which is correct but if you want to separate it and use the same module then you get that module only. If you use [], then you are not using the earlier module and your earlier dependencies will also be not referred correctly.
its not gonna be saved you dont have permission . can you pin point to me the errors please thank you
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.