Skip to content

Instantly share code, notes, and snippets.

@w3cj
Created May 12, 2016 17:13
Show Gist options
  • Select an option

  • Save w3cj/1f53bd33bb245d79c727495756a89699 to your computer and use it in GitHub Desktop.

Select an option

Save w3cj/1f53bd33bb245d79c727495756a89699 to your computer and use it in GitHub Desktop.
var app = angular.module('CameraShop', []);
app.controller('CamerasController', CamerasController);
function CamerasController($scope, CameraService){
$scope.cameras = CameraService.getCameras();
$scope.order = 'rating';
$scope.addCamera = function() {
CameraService.addCamera($scope.newCamera);
}
}
<!DOCTYPE html>
<html ng-app="CameraShop">
<head>
<meta charset="utf-8">
<title>Camera Shop</title>
<link rel="stylesheet" href="https://bootswatch.com/paper/bootstrap.css" media="screen" title="no title" charset="utf-8">
<style media="screen">
.sale {
color: orange;
}
</style>
</head>
<body ng-controller="CamerasController">
<header class="page-header text-center">
<h1>Reddit!</h1>
</header>
<main class="container">
<div class="form-group">
<input type="text" ng-model="newCamera">
<button type="button" class="btn btn-info" ng-click="addCamera()">Add Camera</button>
</div>
<select class="form-control" ng-model="order">
<option value="-rating">Rating</option>
<option value="-price">Price</option>
</select>
<section ng-repeat="camera in cameras | orderBy : order">
<h3>{{::camera.title}}</h3>
<img ng-src="{{::camera.image}}" alt="{{camera.title}}" />
<h4 ng-class="{
sale: camera.onSale,
outOfStock: camera.quantity == 0
}">Price: {{camera.price | currency}} <span ng-show="camera.onSale">ON SALE!!</span></h4>
<h4>Rating: {{camera.rating}}</h4>
</section>
</main>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.js" charset="utf-8"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.js" charset="utf-8"></script>
<script src="app.js" charset="utf-8"></script>
<script src="CameraService.js" charset="utf-8"></script>
</body>
</html>
'use strict';
angular
.module('CameraShop')
.factory('CameraService', CameraService);
function CameraService() {
let self = this;
this.cameras = [
{
title: 'Nikon D3100 DSLR',
image: 'http://ecx.images-amazon.com/images/I/713u2gDQqML._SX522_.jpg',
rating: 3.4,
price: 369.99,
onSale: true
},
{
title: 'Canon EOS 70D',
image: 'http://ecx.images-amazon.com/images/I/81U00AkAUWL._SX522_.jpg',
rating: 2.0,
price: 1099.0,
onSale: false
},
{
title: 'Nikon D810A',
image:'http://ecx.images-amazon.com/images/I/91wtXIfLl2L._SX522_.jpg',
rating: 4.2,
price: 3796.95,
onSale: true
}
];
return {
getCameras: function() {
return self.cameras;
},
addCamera: function(camera) {
self.cameras.push({
title: camera,
image:'http://ecx.images-amazon.com/images/I/91wtXIfLl2L._SX522_.jpg',
rating: 4.2,
price: 3796.95,
onSale: true
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment