我正在学习AngularJS并收到以下错误
angular.js:13920TypeError :CRUDService.ShoppingList_InsertUpdate不是函数
以下是我的控制器
app.controller('addShopItem', ['$scope', '$http', 'CRUDService', 'uiGridConstants', function ($scope, $http, CRUDService, uiGridConstants) {
var apiRoutePost = 'http://localhost:vvvvv/api/ShoppingListAPI/Post/';
$scope.addShoppingItem = function () {
CRUDService.ShoppingList_InsertUpdate(apiRoutePost, $scope.ShoppingList);
};
}]);
和我的CRUDService javascript
app.service('CRUDService', function ($http) {
//**********----Get Shopping List Items----***************
$scope.getShoppingListItems = function (apiRoute) {
return $http.get(apiRoute);
}
//**********----Insert Shopping List Items----***************
$scope.ShoppingItems.InsertUpdate = function (apiRoutePost, data) {
return $http.post(apiRoutePost, data);
}
});
感谢帮助
答案 0 :(得分:0)
这是错误的方式。您不在服务中使用范围,将所有功能绑定到服务,以后可以使用ServiceName.functionName
在控制器中访问。将函数绑定到Service
1.直接绑定服务
this.ShoppingList_InsertUpdate = function (apiRoutePost, data) {
return $http.post(apiRoutePost, data);
}
2。编写简单的函数并以这种方式绑定它们
function ShoppingList_InsertUpdate(apiRoutePost, data){
return $http.post(apiRoutePost, data);
}
this.ShoppingList_InsertUpdate = ShoppingList_InsertUpdate;
并在控制器中
CRUDService.ShoppingList_InsertUpdate(apiRoutePost, $scope.ShoppingList);
答案 1 :(得分:0)
控制器
app.controller('addShopItem', ['$scope', '$http', 'CRUDService', 'uiGridConstants', function ($scope, $http, CRUDService, uiGridConstants) {
var apiRoutePost = 'http://localhost:vvvvv/api/ShoppingListAPI/Post/';
$scope.addShoppingItem = function () {
CRUDService.ShoppingList.InsertUpdate(apiRoutePost, $scope.ShoppingList);
};
}]);
服务
app.service('CRUDService', function ($http) {
this.shoppinListItem = {};
this.getShoppingListItems = {};
//**********----Get Shopping List Items----***************
this.getShoppingListItems = function (apiRoute) {
return $http.get(apiRoute);
}
//**********----Insert Shopping List Items----***************
this.ShoppingItems.InsertUpdate = function (apiRoutePost, data) {
return $http.post(apiRoutePost, data);
}
});