Angularjs Meanstack/Angular.js如何更新单独的模型

Angularjs Meanstack/Angular.js如何更新单独的模型,angularjs,Angularjs,我正在为一个电子商务系统写一个管理系统。应用程序需要根据每个产品/每个类别创建定价规则 所以。。。。。对于具有CategoryID的新价格规则,我希望使用该CategoryID更新所有产品。如何从pricing controller调用所有产品,然后更新它们 我希望定价控制器中的此函数使用表单中设置的CategoryId更新产品 $scope.saveRule = function saveRule(row){ var CategoryId = row.CategoryId;

我正在为一个电子商务系统写一个管理系统。应用程序需要根据每个产品/每个类别创建定价规则

所以。。。。。对于具有CategoryID的新价格规则,我希望使用该CategoryID更新所有产品。如何从pricing controller调用所有产品,然后更新它们

我希望定价控制器中的此函数使用表单中设置的CategoryId更新产品

$scope.saveRule = function saveRule(row){
    var CategoryId =     row.CategoryId;
    if(row.id =="newRecord"){
        var roundup =        $('#roundupnewRecord').val();
        var percentage =     $('#percentagenewRecord').val();
        var pricing = new Pricing({
            CategoryId:     CategoryId,
            roundup:        roundup,
            percentage:     percentage
        });

        pricing.$save(function(response) {
            $route.reload();
        });
    } else {
        Pricing.get({
            pricingId: row.id
        }, function(pricing) {
            pricing.roundup =        $('#roundup'+row.id).val();
            pricing.percentage =     $('#percentage'+row.id).val();
            pricing.$update(function() {
                $route.reload();
            });
        });
    }
}
提前感谢您的帮助

价格控制员-棱角的

'use strict';
angular.module('mean.pricing').controller('PricingController', [ '$route', '$http', '$scope', '$routeParams', '$location', 'Global', 'Pricing', function ($route, $http, $scope, $routeParams, $location, Global, Pricing) {
    $scope.global = Global;

    $scope.create = function() {
        var pricing = new Pricing({
            CategoryId: this.title,
            content: this.content
        });

        pricing.$save(function(response) {
            console.log(response);
            $location.path('pricing/' + response.id);
        });

        this.title = '';
        this.content = '';
    };

    function generateDefaultRule() {

        return {
            CategoryId:         0,
            ProductId:          '',
            roundup:            2,
            percentage:         1,
            newRecord:          1,
            id:                 'newRecord'
        }
    }

    $scope.addRule = function addRule(id) {
        $scope.rowCollection.push(generateDefaultRule());
        console.log();
    };

    $scope.saveRule = function saveRule(row){
        var CategoryId =     row.CategoryId;
        if(row.id =="newRecord"){
            var roundup =        $('#roundupnewRecord').val();
            var percentage =     $('#percentagenewRecord').val();
            var pricing = new Pricing({
                CategoryId:     CategoryId,
                roundup:        roundup,
                percentage:     percentage
            });

            pricing.$save(function(response) {
                $route.reload();
            });
        } else {
            Pricing.get({
                pricingId: row.id
            }, function(pricing) {
                pricing.roundup =        $('#roundup'+row.id).val();
                pricing.percentage =     $('#percentage'+row.id).val();
                pricing.$update(function() {
                    $route.reload();
                });
            });
        }

        //Get Products with Relative CategoryId
    }

    $scope.update = function() {
        var pricing = $scope.pricing;
        if (!pricing.updated) {
            pricing.updated = [];
        }
        pricing.updated.push(new Date().getTime());

        pricing.$update(function() {
            $location.path('pricing/' + pricing.id);
        });
    };

    $scope.find = function() {
        Pricing.query(function(pricing) {
            $scope.pricing = pricing;
        });

    };

    $scope.findOverall = function() {
        $http.get('/Pricing/overall').then(function(pricing) {
            $scope.overall = pricing;
        });

    };

    $scope.findCategories = function() {
        $http.get('/Pricing/categories').then(function(pricing) {
            console.log(pricing);
            $scope.categories = pricing.data;
        });
    };

    $scope.findProducts = function() {
        $http.get('/Pricing/products').then(function(pricing) {
            $scope.products = pricing.data;
        });
    };

    $scope.findOne = function() {
        Pricing.get({
            pricingId: $routeParams.pricingId
        }, function(pricing) {
            $scope.pricing = pricing;
        });
    };

    $scope.remove = function(pricing) {
        if (pricing) {
            pricing.$remove();  

            for (var i in $scope.pricing) {
                if ($scope.pricing[i] === pricing) {
                    $scope.pricing.splice(i, 1);
                }
            }
        }
        else {
            $scope.pricing.$remove();
            $location.path('pricing');
        }
    };

    $scope.removeItem = function removeItem(row) {
        Pricing.get({
            pricingId: row.id
        }, function(pricing) {
            pricing.$remove(function() {
                var index = $scope.rowCollection.indexOf(row);
                if (index !== -1) {
                    $scope.rowCollection.splice(index, 1);
                }     
            });
        });
    }

    $scope.list = function(){
        $('table').on('click', 'a' , function (event) {
            var id = $(this).attr('id');
            if($(this).hasClass('editButton')){
                $('#percentage'+id).css('display','inline-block');
                $('#roundup'+id).css('display','inline-block');
                $('#percentageSpan'+id).css('display','none');
                $('#roundupSpan'+id).css('display','none');
                $('.actionButtonsDiv'+id).css('display','none');
                $('#saveButtonDiv'+id).css('display','inline');
            }
        });

        $http.get('/pricing').then(function(pricing) {
            $scope.rowCollection = pricing.data;
        });

        $http.get('/category').then(function(categories) { 
            $scope.categories = categories.data;
        });
    }

}]);
'use strict';
angular.module('mean.products').controller('ProductsController', ['$http', '$scope', '$routeParams', '$location', 'Global', 'Products', function ($http, $scope, $routeParams, $location, Global, Products) {
    $scope.global = Global;

    $scope.create = function() {
        var product = new Products({
            title: this.title,
            content: this.content
        });

        product.$save(function(response) {

            $location.path("products/" + response.id);
        });

        this.title = "";
        this.content = "";
    };

    $scope.remove = function(product) {
        if (product) {
            product.$remove();  

            for (var i in $scope.products) {
                if ($scope.products[i] === product) {
                    $scope.products.splice(i, 1);
                }
            }
        }
        else {
            $scope.product.$remove();
            $location.path('products');
        }
    };

    $scope.update = function() {
        var product = $scope.product;
        if (!product.updated) {
            product.updated = [];
        }
        product.updated.push(new Date().getTime());

        product.$update(function() {
            $location.path('products/' + product.id);
        });
    };

    $scope.find = function() {
        Products.query(function(products) {
            // console.log(products);
            $scope.products = products;
        });
    };

    $scope.categories = function() {
        var selected = {};
        $('#multiple').on('click', function(){
            $('.product-checkbox').each(function() {
                if ($(this).is(":checked")) {
                    $(this).prop('checked', false);
                }else{
                    $(this).prop('checked', true);
                }
            });  

        });

        $.each( ['approveButton', 'rejectButton', 'multiButton'], function( index, value ){
            $('.'+value).on('click', function(){

                $('.product-checkbox').each(function() {
                    var productId = $(this).attr('id');

                    if ($(this).is(":checked")) {
                        if (value === 'rejectButton') { 
                            var categoryId = 199;
                        }else{
                            var categoryId = $('#selectProduct'+$(this).attr('id')).val().replace('number:','');
                        } 

                        Products.get({
                            productId: productId
                        }, function(product){
                            product.CategoryId = categoryId;

                            product.$update(function(result) {
                            });
                        });
                    }

                    //Approves checked and rejcts unchecked products
                    if (value == 'multiButton') {

                        if (!$(this).is(":checked")) {
                            Products.get({
                                productId: productId
                            }, function(product){
                                product.CategoryId = 199;
                                product.$update(function() {

                                });
                            });
                        }
                    }
                });

                $location.path('products/categories');
            });    
        });


        $http.get('/products/categories').then(function(products) {         
            $scope.products = products.data;
        });

        $http.get('/category').then(function(categories) { 
            $scope.categories = categories.data;
        });

        $http.get('/productCategoryMatchs').then(function(productCategoryMatchs) {      
            var pCMResponse = productCategoryMatchs.data;
            var pcmArray = {};
            for(var index in pCMResponse){
                pcmArray[pCMResponse[index].ProductId] = pCMResponse[index].CategoryId;
            }

            $scope.pCMs = pcmArray;
        });


    };

    $scope.findOne = function() {
        Products.get({
            productId: $routeParams.productId
        }, function(product) {
            $scope.product = product;
        });
    };
}]);
产品控制器-角度控制器

'use strict';
angular.module('mean.pricing').controller('PricingController', [ '$route', '$http', '$scope', '$routeParams', '$location', 'Global', 'Pricing', function ($route, $http, $scope, $routeParams, $location, Global, Pricing) {
    $scope.global = Global;

    $scope.create = function() {
        var pricing = new Pricing({
            CategoryId: this.title,
            content: this.content
        });

        pricing.$save(function(response) {
            console.log(response);
            $location.path('pricing/' + response.id);
        });

        this.title = '';
        this.content = '';
    };

    function generateDefaultRule() {

        return {
            CategoryId:         0,
            ProductId:          '',
            roundup:            2,
            percentage:         1,
            newRecord:          1,
            id:                 'newRecord'
        }
    }

    $scope.addRule = function addRule(id) {
        $scope.rowCollection.push(generateDefaultRule());
        console.log();
    };

    $scope.saveRule = function saveRule(row){
        var CategoryId =     row.CategoryId;
        if(row.id =="newRecord"){
            var roundup =        $('#roundupnewRecord').val();
            var percentage =     $('#percentagenewRecord').val();
            var pricing = new Pricing({
                CategoryId:     CategoryId,
                roundup:        roundup,
                percentage:     percentage
            });

            pricing.$save(function(response) {
                $route.reload();
            });
        } else {
            Pricing.get({
                pricingId: row.id
            }, function(pricing) {
                pricing.roundup =        $('#roundup'+row.id).val();
                pricing.percentage =     $('#percentage'+row.id).val();
                pricing.$update(function() {
                    $route.reload();
                });
            });
        }

        //Get Products with Relative CategoryId
    }

    $scope.update = function() {
        var pricing = $scope.pricing;
        if (!pricing.updated) {
            pricing.updated = [];
        }
        pricing.updated.push(new Date().getTime());

        pricing.$update(function() {
            $location.path('pricing/' + pricing.id);
        });
    };

    $scope.find = function() {
        Pricing.query(function(pricing) {
            $scope.pricing = pricing;
        });

    };

    $scope.findOverall = function() {
        $http.get('/Pricing/overall').then(function(pricing) {
            $scope.overall = pricing;
        });

    };

    $scope.findCategories = function() {
        $http.get('/Pricing/categories').then(function(pricing) {
            console.log(pricing);
            $scope.categories = pricing.data;
        });
    };

    $scope.findProducts = function() {
        $http.get('/Pricing/products').then(function(pricing) {
            $scope.products = pricing.data;
        });
    };

    $scope.findOne = function() {
        Pricing.get({
            pricingId: $routeParams.pricingId
        }, function(pricing) {
            $scope.pricing = pricing;
        });
    };

    $scope.remove = function(pricing) {
        if (pricing) {
            pricing.$remove();  

            for (var i in $scope.pricing) {
                if ($scope.pricing[i] === pricing) {
                    $scope.pricing.splice(i, 1);
                }
            }
        }
        else {
            $scope.pricing.$remove();
            $location.path('pricing');
        }
    };

    $scope.removeItem = function removeItem(row) {
        Pricing.get({
            pricingId: row.id
        }, function(pricing) {
            pricing.$remove(function() {
                var index = $scope.rowCollection.indexOf(row);
                if (index !== -1) {
                    $scope.rowCollection.splice(index, 1);
                }     
            });
        });
    }

    $scope.list = function(){
        $('table').on('click', 'a' , function (event) {
            var id = $(this).attr('id');
            if($(this).hasClass('editButton')){
                $('#percentage'+id).css('display','inline-block');
                $('#roundup'+id).css('display','inline-block');
                $('#percentageSpan'+id).css('display','none');
                $('#roundupSpan'+id).css('display','none');
                $('.actionButtonsDiv'+id).css('display','none');
                $('#saveButtonDiv'+id).css('display','inline');
            }
        });

        $http.get('/pricing').then(function(pricing) {
            $scope.rowCollection = pricing.data;
        });

        $http.get('/category').then(function(categories) { 
            $scope.categories = categories.data;
        });
    }

}]);
'use strict';
angular.module('mean.products').controller('ProductsController', ['$http', '$scope', '$routeParams', '$location', 'Global', 'Products', function ($http, $scope, $routeParams, $location, Global, Products) {
    $scope.global = Global;

    $scope.create = function() {
        var product = new Products({
            title: this.title,
            content: this.content
        });

        product.$save(function(response) {

            $location.path("products/" + response.id);
        });

        this.title = "";
        this.content = "";
    };

    $scope.remove = function(product) {
        if (product) {
            product.$remove();  

            for (var i in $scope.products) {
                if ($scope.products[i] === product) {
                    $scope.products.splice(i, 1);
                }
            }
        }
        else {
            $scope.product.$remove();
            $location.path('products');
        }
    };

    $scope.update = function() {
        var product = $scope.product;
        if (!product.updated) {
            product.updated = [];
        }
        product.updated.push(new Date().getTime());

        product.$update(function() {
            $location.path('products/' + product.id);
        });
    };

    $scope.find = function() {
        Products.query(function(products) {
            // console.log(products);
            $scope.products = products;
        });
    };

    $scope.categories = function() {
        var selected = {};
        $('#multiple').on('click', function(){
            $('.product-checkbox').each(function() {
                if ($(this).is(":checked")) {
                    $(this).prop('checked', false);
                }else{
                    $(this).prop('checked', true);
                }
            });  

        });

        $.each( ['approveButton', 'rejectButton', 'multiButton'], function( index, value ){
            $('.'+value).on('click', function(){

                $('.product-checkbox').each(function() {
                    var productId = $(this).attr('id');

                    if ($(this).is(":checked")) {
                        if (value === 'rejectButton') { 
                            var categoryId = 199;
                        }else{
                            var categoryId = $('#selectProduct'+$(this).attr('id')).val().replace('number:','');
                        } 

                        Products.get({
                            productId: productId
                        }, function(product){
                            product.CategoryId = categoryId;

                            product.$update(function(result) {
                            });
                        });
                    }

                    //Approves checked and rejcts unchecked products
                    if (value == 'multiButton') {

                        if (!$(this).is(":checked")) {
                            Products.get({
                                productId: productId
                            }, function(product){
                                product.CategoryId = 199;
                                product.$update(function() {

                                });
                            });
                        }
                    }
                });

                $location.path('products/categories');
            });    
        });


        $http.get('/products/categories').then(function(products) {         
            $scope.products = products.data;
        });

        $http.get('/category').then(function(categories) { 
            $scope.categories = categories.data;
        });

        $http.get('/productCategoryMatchs').then(function(productCategoryMatchs) {      
            var pCMResponse = productCategoryMatchs.data;
            var pcmArray = {};
            for(var index in pCMResponse){
                pcmArray[pCMResponse[index].ProductId] = pCMResponse[index].CategoryId;
            }

            $scope.pCMs = pcmArray;
        });


    };

    $scope.findOne = function() {
        Products.get({
            productId: $routeParams.productId
        }, function(product) {
            $scope.product = product;
        });
    };
}]);
产品控制器-节点

'use strict';

/**
 * Module dependencies.
 */
var StandardError = require('standard-error');
var db = require('../../config/sequelize');

/**
 * Find product by id
 * Note: This is called every time that the parameter :productId is used in a URL. 
 * Its purpose is to preload the product on the req object then call the next function. 
 */
exports.product = function(req, res, next, id) {
    console.log('id => ' + id);
    db.Product.find({ where: {id: id}}).then(function(product){
        if(!product) {
            return next(new Error('Failed to load product ' + id));
        } else {
            req.product = product;
            return next();            
        }
    }).catch(function(err){
        return next(err);
    });
};

/**
 * Create a product
 */
exports.create = function(req, res) {
    // augment the product by adding the UserId
    req.body.UserId = req.user.id;
    // save and return and instance of product on the res object. 
    db.Product.create(req.body).then(function(product){
        if(!product){
            return res.send('users/signup', {errors: new StandardError('Product could not be created')});
        } else {
            return res.jsonp(product);
        }
    }).catch(function(err){
        return res.send('users/signup', { 
            errors: err,
            status: 500
        });
    });
};

/**
 * Update a product
 */
exports.update = function(req, res) {
    // create a new variable to hold the product that was placed on the req object.
    var product = req.product;

    product.updateAttributes({
        price:              req.body.price,
        CategoryId:         req.body.CategoryId
    }).then(function(a){
        return res.jsonp(a);
    }).catch(function(err){
        return res.render('error', {
            error: err, 
            status: 500
        });
    });
};

/**
 * Delete an product
 */
exports.destroy = function(req, res) {
    // create a new variable to hold the product that was placed on the req object.
    var product = req.product;

    product.destroy().then(function(){
        return res.jsonp(product);
    }).catch(function(err){
        return res.render('error', {
            error: err,
            status: 500
        });
    });
};

/**
 * Show an product
 */
exports.show = function(req, res) {
    // Sending down the product that was just preloaded by the products.product function
    // and saves product on the req object.
    return res.jsonp(req.product);
};

/**
 * List of Products
 */
exports.all = function(req, res) {
    db.Product.findAll({}).then(function(products){
        return res.jsonp(products);
    }).catch(function(err){
        return res.render('error', {
            error: err,
            status: 500
        });
    });
};


/**
 * List of Products
 */
exports.list = function(req, res) {
    db.Product.findAll({
        limit : 20
    }).then(function(products){
        return res.jsonp(products);
    }).catch(function(err){
        return res.render('500', {
            error: err,
            status: 500
        });
    });
};

/**
 * List of Products and there categories
 */
exports.categories = function(req, res) {

    db.Product.findAll({
        attributes : [
            'name',
            'id',
            // 'ProductCategoryMatch.count'
        ],
        where: {
            CategoryId : null
        },
        // include : [
        //     { model: db.ProductCategoryMatch }
        // ],
        // order : [
        // ]
        limit: 20
    }).then(function(products){
        return res.jsonp(products);
    }).catch(function(err){
        return res.render(500, {
            error: err,
            status: 500
        });
    });
};

/**
 * Article authorizations routing middleware
 */
exports.hasAuthorization = function(req, res, next) {


    // if (req.product.User.id !== req.user.id) {
    //   return res.send(401, 'User is not authorized');
    // }
    next();
};

真的不清楚你在问什么。。。将问题缩小到更具体的问题感谢charlietfl我已经更新了问题,如果问题仍然不清楚,请告诉我。虽然您似乎正在使用jQuery获取不寻常的值,并且没有利用
ng model
2路表单绑定,但对于您遇到的问题,并没有更多的细节控制汉克斯!还在学习,已经拿出jquery并使用了ng模型。更好/更容易!我基本上是想从pricings控制器调用Products update函数。可能吗?