【问题标题】:Angularjs service keep getting object is not a function errorAngularjs服务不断获取对象不是函数错误
【发布时间】:2014-02-26 13:57:02
【问题描述】:

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

但我不断收到错误 TypeError: object is not a function at 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'];

谁能帮我解决这个问题?提前致谢!

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    首先

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

    【讨论】:

    • 谢谢!。这些变化是我错过的
    猜你喜欢
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 2013-04-15
    • 2015-10-08
    • 1970-01-01
    • 2019-08-03
    相关资源
    最近更新 更多