Angularjs服务不断获取对象不是函数错误

时间:2014-02-26 05:46:30

标签: angularjs

我正在为项目使用角度js,我有两个控制器。在newCustomerController中创建了一个新客户,我需要访问另一个控制器中的客户详细信息。我正在使用工厂执行上述请求。

但我一直收到错误TypeError:object不是addCustomer函数的函数。

我的工厂如下

 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;
 });

我的第一个控制器如下

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);
        });
    };
}

此控制器创建客户。成功后,它将在另一个控制器中设置客户的范围。我的控制器如下

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;
});
};

注射也是如此

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

任何人都可以帮我这个吗?提前谢谢!

1 个答案:

答案 0 :(得分:2)

首先

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;
});

我没有在你的控制器中看到注入mySharedService

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

在这里你可能想要分配客户?

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