【发布时间】: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