2016-02-28 118 views
0

試圖縮小我的angularjs代碼,但不知何故它會在我將它縮小時發生錯誤。如果不進行縮小,它就像預期的一樣工作,沒有錯誤。最小化angularjs腳本導致錯誤

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); 
    } 
}); 

任何想法,我做錯了嗎?

+1

http://stackoverflow.com/questions/18782324/angularjs-minify-best-practice,https://docs.angularjs.org/guide/di –

+0

感謝@ JBNizet,解決了它。應該找到我自己。 – RvdM

回答

1

爲了縮小角度,您必須將需要注入項的函數包裝到包含依賴項的字符串表示的數組中。

也許這會有所幫助。 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); 
 
    } 
 
}]);

+0

謝謝,這的確是! – RvdM