2016-08-02 97 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