gpt4 book ai didi

javascript - 创建允许用户添加或删除项目的服务

转载 作者:行者123 更新时间:2023-11-28 18:33:18 24 4
gpt4 key购买 nike

我正在尝试创建一个充当购物篮的服务,允许用户添加/删除项目。但是,我遇到以下错误消息无法读取未定义的属性“addServiceItem”。这是到目前为止我的代码:

controller.js

$scope.addServiceItem = function(bookingSelected, title, price, length) {
if (bookingSelected === true) {
BusinessService.manageServiceItems.addServiceItemcont(title, price, length);
}
}

services.js

function BusinessService($http) {

function manageServiceItems() {

var serviceItem = [];

function addServiceItem(title, price, length) {
serviceItem.push({
title: title,
price: price,
length: length
});
}

this.getServiceItem = function() {
return serviceItem;
}

}

最佳答案

服务定义应该返回一个对象或函数。来自 docs

The service factory function generates the single object or function that represents the service to the rest of the application. The object or function returned by the service is injected into any component (controller, service, filter or directive) that specifies a dependency on the service.

function BusinessService($http) {
var BusinessService = this, serviceItem = [];

BusinessService.manageServiceItems = {
addServiceItem: function(title, price, length) {
serviceItem.push({
title: title,
price: price,
length: length
});
},
getServiceItem: function() {
return serviceItem;
}
}
return BusinessService;
};

var myModule = angular.module('myModule', [])
.factory('BusinessService', BusinessService);

关于javascript - 创建允许用户添加或删除项目的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37576087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com