AngularJS - ng-switch and primitive
by darul75
HTML
<div ng-controller="Controller">
<h1>
<span class="btn btn-default btn-large">{{order.total}} €</span>
</h1>
<div data-id="{{package.productId}}" class="btn bt-default panel panel-default order order-card withripple" ng-click="addProductToCurrentOrder($parent.$index, package.productId)">
<div class="panel-body">
<p class="lead"> {{package.name}}</p>
<p>{{package.weight}} {{package.unit}}</p>
</div>
<div class="ripple-wrapper"></div>
</div>
</div>
JavaScript
var app = angular.module('myApp', [])
.controller('Controller', ['$scope', function(scope) {
// Mock
var Globalize = {
format : function(a,b) {
return 22;
}
} ;
// Or I will suggest compute total when computing total
scope.order = {
_total : 0,
total : 22,
positions: []
};
scope.addProductToCurrentOrder = function(packageIndex, productId) {
var rs = {
_id : productId
};
var tab = $scope.categories[packageIndex].packages;
for (var i = 0; i < tab.length; i++) {
var pack = tab[i];
if (pack.productId === productId){
pack.quantity++;
rs.articleName = pack.name;
rs.price = pack.price;
rs._price = pack._price;
rs.unit = pack.unit;
rs.weight = pack.weight;
break;
}
}
$scope.order.positions.push(rs);
$scope.order._total += rs._price;
$scope.order.total = Globalize.format($scope.order._total,"n");
};
}])
.directive('directive', function() {
return {
// scope = true, we have one new isolated scope
scope: {
customerInfo: '=info'
},
restrict: 'E',
template: '<div>Name: {{customerInfo.name}} Address: {{customerInfo.address}}</div><div>Name: {{john.name}} Address: {{john.address}}</div>',
link: function (scope, element) {
console.log(scope);
}
};
});