0

I am using angular js for a project and I have two controllers. A new customer is created in the newCustomerController and I need to access the customer details in another controller. I am using the factory to perform the above request.

But I keep getting the error TypeError: object is not a function at the addCustomer function.

My factory as below

 app.factory('mySharedService',function($rootScope){
    var sharedService = {};

   sharedService.customer = {};

   sharedService = {

       prepLoadAddress : function(customer){
        this.customer = customer;
        $this.loadAddress();
      },

      loadAddress : function(){
        $rootScope.$broadcast('handleLoadAddress');
      }
   };
   return sharedService;
 });

My first controller as below

function newCustomerController($scope,$http){
     $scope.name = "John Smith";
     $scope.email = "John Smith";
     $scope.contact = "John Smith";
     $scope.address = "John Smith";
     $scope.comment = "John Smith";
     $scope.archived = 0;

     $scope.addCustomer = function() {

        $http({
            method: 'POST',
            url: '/customers/createCustomer',
            params: {
                name: $scope.name,
                email: $scope.email,
                contact: $scope.contact,
                delivery_comment: $scope.comment,
                archived: $scope.archived
            }
        }).success(function(result) {
            $scope.name = "";
            $scope.email = "";
            $scope.contact = "";
            $scope.address = "";
            $scope.comment = "";
            $scope.archived = 0;

            mySharedService.prepLoadAddress(result);
        });
    };
}

This controller creates the customer. Upon success, it will set the scope for the customer in another controller. My controller as below

function bController($scope, $http) {
$scope.customer = {};
    $scope.load_customer = function() {

    if ($scope.customer_id != null) {
        $http({
            method: 'GET',
            url: '/customers/customer',
            params: {
                id: $scope.customer_id
            }
        }).success(function(result) {
            $scope.customer = result;
            $scope.address_id = result.address.id;
            $scope.address = result.address;
        });
    }
};

$scope.$on('handleLoadAddress',function(events,customer){
    $scope.customer = mySharedService;
});
};

Did the inject as well

newCustomerController.$inject = ['$scope','mySharedService'];
newWaypointController.$inject = ['$scope','mySharedService'];

Anyone can help me with this? Thanks in advance!

1 Answer 1

2

First of all

app.factory('mySharedService',function($rootScope){
  var sharedService = {};

  sharedService.customer = {};   

  sharedService = {          <-- here you assign new object to sharedService, so there is no sense in previous two lines.

    prepLoadAddress : function(customer){
      this.customer = customer;
      $this.loadAddress();
    },

    loadAddress : function(){
      $rootScope.$broadcast('handleLoadAddress');
    }
  };
  return sharedService;
});

I don't see in your controllers injection of mySharedService

function newCustomerController($scope,$http, mySharedService){
function bController($scope, $http, mySharedService) {

And here you probably want to assign customer?

$scope.$on('handleLoadAddress',function(events,customer){
   $scope.customer = mySharedService.customer;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!. Those changes were what I miss out on

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.