Skip to content

Instantly share code, notes, and snippets.

@aaronlelevier
Created April 23, 2015 12:49
Show Gist options
  • Select an option

  • Save aaronlelevier/90da27ca7336c775dc1d to your computer and use it in GitHub Desktop.

Select an option

Save aaronlelevier/90da27ca7336c775dc1d to your computer and use it in GitHub Desktop.
AngularJS Controller Example
define([
'./module'
], function(module) {
'use strict';
module.controller('NewsletterCtrl', ['$scope', function($scope) {
$scope.dj_test = "`test`: angular is loaded";
}]);
module.controller('PricingCtrl', ['$scope', 'Pricing', function($scope, Pricing) {
//prices will be from a DRF REST endpoint, so when changed in the DB,
//they are auto-reflected here
$scope.prices = Pricing.query();
$scope.volumes = [500, 2500, 5000, 7500];
$scope.volume = "";
$scope.submit_volume = function(volume) {
$scope.volume = volume;
}
// calculate SMS cost
$scope.calc_cost = function() {
$scope.cost = 0;
angular.forEach($scope.prices, function(p) {
if ($scope.volume >= p.end) {
$scope.cost += p.price * (p.end - p.start + 1);
} else if ($scope.volume >= p.start && $scope.volume < p.end) {
$scope.cost += p.price * ($scope.volume - p.start + 1);
}
});
return $scope.cost;
}
$scope.$watch(
'volume',
function(newValue) {
return newValue;
});
}]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment