2016-08-02 95 views
-1

我学习AngularJS并收到以下错误13920类型错误在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

看看构建'services'的正确方法 - https://docs.angularjs.org/guide/services。范围的目的是将应用程序的表示和业务逻辑“粘合在一起”。将$范围传递给服务没有多大意义。避免在服务级别使用'$ scope'。 –

+0

我不认为你正在创建你的服务。 $范围从哪里来?自从我写了一篇,但看看文档已经有一段时间了。 –

回答

0

这是走错了路。您不使用服务范围,您将所有功能绑定到服务和更高版本可以通过使用ServiceName.functionName访问控制器。有绑定功能服务

1.Binding以服务直接

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); 
+0

非常感谢你的款待 – mspelly

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

});

+0

非常感谢 - 效果很好 – mspelly