AngularJS 카트 구현
html>
`item`.`id` `item`.`name`
`item`.`price` {{item.quantity*item.price}}
:{{totalPrice()}} :{{totalQuantity()}}
angular.module('app', []).controller('firstController',
function($scope) {
$scope.Product = [{
id: 1000,
name: "iPhone 6 Plus",
quantity: 1,
price: 6888
}, {
id: 1001,
name: "iPhone 6",
quantity: 1,
price: 5288
}, {
id: 1002,
name: "iPhone 5s",
quantity: 1,
price: 4188
}, {
id: 1003,
name: "iPhone 5c",
quantity: 1,
price: 3288
}];
$scope.totalPrice = function() {
var total = 0;
angular.forEach($scope.Product, function(item) {
total += item.quantity * item.price;
});
return total;
}
$scope.totalQuantity = function() {
var total = 0;
angular.forEach($scope.Product, function(item) {
total += parseInt(item.quantity);
});
return total;
}
$scope.remove = function(index) {
$scope.Product.splice(index, 1);
}
$scope.removeall = function() {
var index;
for (index = $scope.Product.length - 1; index >= 0; index--) {
$scope.remove(index);
}
}
$scope.reduce = function(index) {
if ($scope.Product[index].quantity != 1) {
$scope.Product[index].quantity--;
} else {
var ans = confirm(" ?");
if (ans) {
$scope.remove(index);
} else {
$scope.Product[index].quantity = 1;
}
}
}
$scope.add = function(index) {
$scope.Product[index].quantity++;
}
$scope.$watch('Product', function(newValue, oldValue) {
angular.forEach(newValue, function(item, key) {
if (item.quantity < 1) {
var ans = confirm(" ?");
if (ans) {
$scope.remove(key);
} else {
item.quantity = oldValue[key].quantity;
}
return;
}
});
}, true);
});
n-repeat는 순환을 실현하고,ng-click 뒤에서 클릭한 후 터치하는 이벤트는first Controller 내의 몇 가지 함수는 자신이 쓴 것입니다.
이곳에서는 카트 상품의 증감을 실현하고 총가와 건수가 이에 따라 변화하며 상품은 제거할 수 있고 카트는 비울 수 있다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
AngularJS 및 Zend 2 프레임워크로 편안한 애플리케이션 만들기 (2)Doctrine은 PHP용으로 매우 인기 있는 데이터 액세스 솔루션으로, 저수준 DBAL 및 고수준 OR 매핑 솔루션을 포함합니다. Doctrine에는 Zend 기반 프로젝트에서 Doctrine을 지원하는 몇 가지 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.