缩小angularjs脚本会导致错误 [英] Minify angularjs script results in error

查看:111
本文介绍了缩小angularjs脚本会导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试最小化我的angularjs代码,但是当我最小化它时,它却以某种方式给出错误.不缩小时,它会按预期工作,并且没有错误.

Trying to minify my angularjs code but somehow it gives an error when I have it minified. When not minifying it works like intended and without an error.

Error: $injector:unpr
Unknown Provider
Unknown provider: aProvider <- a <- CartController

这是代码本身:

var kvv = angular.module('kvv', ['ngStorage']);
kvv.controller('CartController', function($scope, $localStorage, $sessionStorage, $timeout) {

    if ($localStorage.items === undefined) {
        $localStorage.items = [];
    }

    $scope.localStorage = $localStorage

    $scope.remove = function(index) {
      $localStorage.items.splice(index, 1);
    };

    $scope.checked = false;

    $scope.addToCart = function(index, title, desc, price, timeout) {

      $scope.checked = true;
      var found = false;
      angular.forEach($localStorage.items, function(items) {
          if (items.id  === index) {
            $timeout(function() {
              (items.quantity++);
              $scope.checked = false;
            }, 750);
            found = true;
        }
      });
      if (!found) {
        $timeout(function() {
            $localStorage.items.push(angular.extend({
            id: index,
            title: title,
            quantity: 1,
            price: price}, index))
            $scope.checked = false;
            },750);
        }
    };

    $scope.itemWithoutBtw = function(index) {
        var itemWithoutBtw = 0;
        angular.forEach($localStorage.items, function(items) {
            itemWithoutBtw += items.price / 106 * 100;
        })
        return itemWithoutBtw;
    };

    $scope.total = function(index) {
            var total = 0;
            angular.forEach($localStorage.items, function(items) {
                total += items.quantity * items.price;
            })
            return total;
    };

    $scope.totalBtw = function(index) {
            var totalBtw = 0;
            var total = $scope.total();
            angular.forEach($localStorage.items, function(items) {
                totalBtw = total / 106 * 6;
            })
            return totalBtw;
    };

    $scope.totalWithBtw = function(index) {
            var totalWithBtw = 0;
            angular.forEach($localStorage.items, function(items) {
                totalWithBtw += (items.quantity * items.price) + (items.quantity * items.price  / 100 * 6);
            })
            return totalWithBtw;
    };
    $scope.totalWithoutBtw = function(index) {
            var total = $scope.total();
            var totalBtw = $scope.totalBtw();
            var totalWithoutBtw = 0;
            angular.forEach($localStorage.items, function(items) {
                totalWithoutBtw = total - totalBtw;
            })
            return totalWithoutBtw;
    };

    $scope.orderTotal = function(index) {
            var orderTotal = 0;
            angular.forEach($localStorage.items, function(items) {
                orderTotal += items.quantity;
            })
            return orderTotal;
    };

    $scope.orderDelete = function(index) {
        delete $localStorage.items;
        $localStorage.items = [];
        $('html, body').animate({scrollTop: 0}, 2500);
    }
});

知道我在做什么错吗?

推荐答案

要缩小角度,您必须将期望注入项的函数包装在包含依赖项的字符串表示形式的数组中.

To minify in angular, you'll have to wrap the function that expects injected items in an array that contains the string representation of the dependencies.

也许这会有所帮助. https://scotch.io/tutorials/declaring-angularjs-modules-for-minification

var kvv = angular.module('kvv', ['ngStorage']);
//You needed to add the array syntax.
kvv.controller('CartController', ['$scope', '$localStorage', '$sessionStorage', '$timeout', function($scope, $localStorage, $sessionStorage, $timeout) {

    if ($localStorage.items === undefined) {
        $localStorage.items = [];
    }

    $scope.localStorage = $localStorage

    $scope.remove = function(index) {
      $localStorage.items.splice(index, 1);
    };

    $scope.checked = false;

    $scope.addToCart = function(index, title, desc, price, timeout) {

      $scope.checked = true;
      var found = false;
      angular.forEach($localStorage.items, function(items) {
          if (items.id  === index) {
            $timeout(function() {
              (items.quantity++);
              $scope.checked = false;
            }, 750);
            found = true;
        }
      });
      if (!found) {
        $timeout(function() {
            $localStorage.items.push(angular.extend({
            id: index,
            title: title,
            quantity: 1,
            price: price}, index))
            $scope.checked = false;
            },750);
        }
    };

    $scope.itemWithoutBtw = function(index) {
        var itemWithoutBtw = 0;
        angular.forEach($localStorage.items, function(items) {
            itemWithoutBtw += items.price / 106 * 100;
        })
        return itemWithoutBtw;
    };

    $scope.total = function(index) {
            var total = 0;
            angular.forEach($localStorage.items, function(items) {
                total += items.quantity * items.price;
            })
            return total;
    };

    $scope.totalBtw = function(index) {
            var totalBtw = 0;
            var total = $scope.total();
            angular.forEach($localStorage.items, function(items) {
                totalBtw = total / 106 * 6;
            })
            return totalBtw;
    };

    $scope.totalWithBtw = function(index) {
            var totalWithBtw = 0;
            angular.forEach($localStorage.items, function(items) {
                totalWithBtw += (items.quantity * items.price) + (items.quantity * items.price  / 100 * 6);
            })
            return totalWithBtw;
    };
    $scope.totalWithoutBtw = function(index) {
            var total = $scope.total();
            var totalBtw = $scope.totalBtw();
            var totalWithoutBtw = 0;
            angular.forEach($localStorage.items, function(items) {
                totalWithoutBtw = total - totalBtw;
            })
            return totalWithoutBtw;
    };

    $scope.orderTotal = function(index) {
            var orderTotal = 0;
            angular.forEach($localStorage.items, function(items) {
                orderTotal += items.quantity;
            })
            return orderTotal;
    };

    $scope.orderDelete = function(index) {
        delete $localStorage.items;
        $localStorage.items = [];
        $('html, body').animate({scrollTop: 0}, 2500);
    }
}]);

这篇关于缩小angularjs脚本会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆