如何在删除

时间:2017-02-05 20:07:26

标签: javascript angularjs ionic-framework

我正在开发一个Angular应用程序。

我想通过我的服务从我的json数据库中删除对象,问题是在创建我的数据库时,我手动为每个对象提供了一个ID属性并通过我的应用程序使用它。

现在当我通过我的服务从json文件中删除对象时,我需要手动重新排序id,如果不是其他对象的id属性仍然是无序的。

这个问题有实际解决方案吗?

谢谢。

db.json:

 {
    "dishes": [
        {
            "id": 0
            , "name": "Uthapizza"
            , "image": "images/uthapizza.png"
            , "category": "mains"
            , "label": "Hot"
            , "price": "4.99"
            , "description": "A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer."

    }

    , {
        "id": 1
        , "name": "Zucchipakoda"
        , "image": "images/zucchipakoda.png"
        , "category": "appetizer"
        , "label": ""
        , "price": "1.99"
        , "description": "Deep fried Zucchini coated with mildly spiced Chickpea flour batter accompanied with a sweet-tangy tamarind sauce"
}

    , {
        "id": 2
        , "name": "Vadonut"
        , "image": "images/vadonut.png"
        , "category": "appetizer"
        , "label": "New"
        , "price": "1.99"
        , "description": "A quintessential ConFusion experience, is it a vada or is it a donut?"

}`]}`

在我的services.js

.factory('menuFactory', ['$resource', 'baseURL', function ($resource, baseURL) {
        return $resource(baseURL + "dishes/:id", null, {
            'update': {
                method: 'PUT'
            }
        });

        }])

删除代码:

menuFactory.delete({
                        id: dish.id
                    }, function (resp) { 
                } else {
                }
            });

添加菜单代码:

.controller('Adddish', ['$scope', 'dishFactory', 'dishes', function ($scope, dishFactory, dishes) {
                $scope.dishes = dishes;
                $scope.length = $scope.dishes.length
                $scope.new_dish = new menuFactory({
                    id: $scope.length
                    , name: ""
                    , image: ""
                    , category: ""
                    , label: ""
                    , price: ""
                    , description: ""

                });
                $scope.addDish = function () {
                        $scope.new_dish.$save(function () {
                        });
                        $scope.new_dish = new menuFactory({
                            id: $scope.length
                            , name: ""
                            , image: ""
                            , category: ""
                            , label: ""
                            , price: ""
                            , description: ""
                        });

0 个答案:

没有答案