app.controller('dashboardCtrl', ['$scope','$rootScope', function ($scope, $rootScope) {
//Color
$scope.Color = [{
colorname: "Red",
number: "(3)"
}, {
colorname: "Brown",
number: "(9)"
}, {
colorname: "yellow",
number: "(5)"
}, {
colorname: "Violet",
number: "(6)"
}, {
colorname: "Orange",
number: "(1)"
}, {
colorname: "Blue",
number: "(4)"
}]
//price
$scope.Price = [{
pricename: "Below $ 100",
}, {
pricename: "$ 100-500",
}, {
pricename: "$ 1k-10k",
}, {
pricename: "$ 10k-20k",
}, {
pricename: "$ Above 20k",
}]
//Product1
$scope.Products1 = [{
url: "images/3.jpg",
id: 18,
category: "Samsung Galaxy (3)",
Name: "Samsung Galaxy (3)",
Price: "300",
qty: 1,
showAddToCart: false,
addedToCart: false
},
{
url: "images/4.jpg",
id: 21,
category: "Samsung Galaxy (5)",
Name: "Samsung Galaxy (5)",
Price: "400",
qty: 1,
showAddToCart: false,
addedToCart: false
},
{
url: "images/5.jpg",
id: 23,
category: "Samsung Galaxy (4)",
Name: "Samsung Galaxy (4)",
Price: "350",
qty: 1,
showAddToCart: false,
addedToCart: false
},
{
url: "images/6.jpg",
id: 24,
category: "Nexus",
Name: "Nexus",
Price: "120",
qty: 1,
showAddToCart: false,
addedToCart: false
}
];
//Product 3
$scope.Products3 = [{
url: "images/8.jpg",
id: 19,
category: "G Speaker",
Name: "G Speaker",
Price: "28",
qty: 1,
showAddToCart: false,
addedToCart: false
},
{
url: "images/18.jpg",
id: 82,
category: "Hp PURE",
Name: "Hp PURE",
Price: "100",
qty: 1,
showAddToCart: false,
addedToCart: false
},
{
url: "images/7.jpg",
id: 37,
category: "G5 Mobile",
Name: "G5 Mobile",
Price: "60",
qty: 1,
showAddToCart: false,
addedToCart: false
},
{
url: "images/6.jpg",
id: 54,
category: "Nexus",
Name: "Nexus",
Price: "220",
qty: 1,
showAddToCart: false,
addedToCart: false
}
];
//product2
$scope.Products2 = [{
url: "images/11.jpg",
id: 10,
category: "Hp Pavilion g6",
Name: "Hp Pavilion g6",
Price: "700",
qty: 1,
showAddToCart: false,
addedToCart: false
},
{
url: "images/12.jpg",
id: 20,
category: "Hp Ivy Brigh",
Name: "Hp Ivy Brigh",
Price: "900",
qty: 1,
showAddToCart: false,
addedToCart: false
},
{
url: "images/9.jpg",
id: 30,
category: "Samsung Headphone",
Name: "Samsung Headphone",
Price: "100",
qty: 1,
showAddToCart: false,
addedToCart: false
},
{
url: "images/10.jpg",
id: 40,
category: "MP3 Player",
Name: "MP3 Player",
Price: "90",
qty: 1,
showAddToCart: false,
addedToCart: false
},
];
//MyCart
$scope.myCartItems = [];
$scope.addToCart = function (item) {
item.qty++;
$rootScope.$broadcast('addProduct', item.Price);
$scope.myCartItems.push(item);
}
$scope.removeItem = function (index) {
$scope.myCartItems.splice(index, 1);
}
}]);
html
<body>
<!-- banner -->
<div class="banner banner1">
<div class="container">
<h2>Great Offers on
<span>Mobiles</span> Flat
<i>35% Discount</i>
</h2>
</div>
</div>
<!-- breadcrumbs -->
<div class="breadcrumb_dress">
<div class="container">
<ul>
<li>
<span class="glyphicon glyphicon-home" aria-hidden="true"></span> Dashboard</a>
</li>
</ul>
</div>
</div>
<!-- //breadcrumbs -->
<!-- mobiles -->
<div class="mobiles">
<div class="container">
<div class="w3ls_mobiles_grids">
<div class="col-md-4 w3ls_mobiles_grid_left">
<div class="w3ls_mobiles_grid_left_grid">
<h3>Categories</h3>
</div>
<div class="w3ls_mobiles_grid_left_grid">
<h3>Color</h3>
<div class="w3ls_mobiles_grid_left_grid_sub">
<div class="ecommerce_color" ng-repeat="x in Color ">
<ul>
<li>
<a href="javascript:void[0]">
<i></i> {{x.colorname+x. number}}</a>
</li>
</ul>
</div>
</div>
</div>
<div class="w3ls_mobiles_grid_left_grid">
<h3>Price</h3>
<div class="w3ls_mobiles_grid_left_grid_sub">
<div class="ecommerce_color ecommerce_size" ng-repeat="y in Price ">
<ul>
<li>{{y.pricename}}</li>
</ul>
</div>
</div>
</div>
</div>
<div class="col-md-8 w3ls_mobiles_grid_right">
<!-- <div id='z'>{{ze}}</div> -->
<div class="col-md-6 w3ls_mobiles_grid_right_left">
<div class="w3ls_mobiles_grid_right_grid1">
<img src="images/46.jpg" alt=" " class="img-responsive" />
<div class="w3ls_mobiles_grid_right_grid1_pos1">
<h3>Smart Phones
<span>Up To</span> 15% Discount</h3>
</div>
</div>
</div>
<div class="col-md-6 w3ls_mobiles_grid_right_left">
<div class="w3ls_mobiles_grid_right_grid1">
<img src="images/47.jpg" alt=" " class="img-responsive" />
<div class="w3ls_mobiles_grid_right_grid1_pos">
<h3>Top 10 Latest
<span>Mobile </span>& Accessories</h3>
</div>
</div>
</div>
<div class="w3ls_mobiles_grid_right_grid2">
<div class="w3ls_mobiles_grid_right_grid2_left">
<h3>Showing Results: 0-1</h3>
</div>
<div class="w3ls_mobiles_grid_right_grid2_right">
<p>Filtering Inputs </p>
<input type="text" ng-model="search">
</div>
<div class="clearfix"> </div>
</div>
<div class="col-md-4 col-sm-4 col-lg-4">
<div ng-repeat="item in Products1 | filter:search | orderBy:'category' track by $index">
<img ng-src="{{item.url}}">
<br> {{item.category}}
<br>
<label>Price</label>
{{item.Price | currency}}
<br>
<input type="number" ng-model="item.qty" min="1" class="qty">
<br>
<label>Total</label> {{ item.qty * item.Price | currency }}
<br>
<div class="simpleCart_shelfItem">
<button type="submit" class="w3ls-cart" ng-click="addToCart(item)" value="Save Data">Add to cart</button>
<br>
<br>
</div>
</div>
</div>
<div class="col-md-4 col-sm-4 col-lg-4">
<div ng-repeat="item in Products2 | filter:search | orderBy:'category'">
<img ng-src="{{item.url}}">
<br> {{item.category}}
<br>
<label>Price</label>
{{item.Price | currency}}
<br>
<input type="number" ng-model="item.qty" min="1" class="qty">
<br>
<label>Total</label> {{ item.qty * item.Price | currency }}
<br>
<div class="simpleCart_shelfItem">
<button type="submit" class="w3ls-cart" ng-click="addToCart(item)" value="Save Data">Add to cart</button>
<br>
<br>
</div>
</div>
</div>
<div class="col-md-4 col-sm-4 col-lg-4">
<div ng-repeat="item in Products3 | filter:search | orderBy:'id'">
<img ng-src="{{item.url}}">
<br> {{item.category}}
<br>
<label>Price</label>
{{item.Price | currency}}
<br>
<input type="number" ng-model="item.qty" min="1" class="qty">
<br>
<label>Total</label> {{ item.qty * item.Price | currency }}
<br>
<div class="simpleCart_shelfItem">
<button type="submit" class="w3ls-cart" ng-click="addToCart(item)" value="Save Data">Add to cart</button>
<br>
<br>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- footer -->
<div class="footer">
<div class="container">
<div class="clearfix"> </div>
<div class="footer-copy">
<div class="footer-copy1">
<div class="footer-copy-pos">
<a href="javascript:void[0]" class="scroll">
<img src="images/arrow.png" alt=" " class="img-responsive" />
</a>
</div>
</div>
<h2>Cart</h2>
<div ng-repeat="product in myCartItems">
{{product.qty + ' x ' +product.category+'= Total '}}{{product.Price*product.qty | currency}}
<a href ng:click="removeItem($index)">X</a>
</div>
<div class="container">
<p>© 2018 Store. All rights reserved
</p>
</div>
</div>
</div>
<!-- //footer -->
</body>
答案 0 :(得分:1)
如果您想要一个if条件,是什么阻止您添加一个条件?
$scope.addToCart = function (item) {
if( /* some conditions are met */ ) {
//do something
} else {
//do something else
}
item.qty++;
$rootScope.$broadcast('addProduct', item.Price);
$scope.myCartItems.push(item);
}