I am working my way through setting up a factory. With help from here I have moved several functions from my OrderFormController to a factory. I have ran into a new problem regarding a function that uses forEach to go through a dynamic ng-repeat array. here is what i have..
code of OrderFormController that holds the function i'm trying to move..
app.controller('OrderFormController', function($scope) {
$scope.total = function(){
var total = 0;
var dtotal = 0;
var ftotal = 0;
angular.forEach($scope.items.results, function(s){
if (s.active){
dtotal+= s.qty * s.price;
}
});
angular.forEach($scope.options.results, function(s){
if (s.active){
ftotal+= s.price;
}
});
total = dtotal + ftotal;
return total;
};
});
here is the factory
app.factory('OrderData', function() {
var OrderFactory = {};
OrderFactory.total = function(){
var total = 0;
angular.forEach(item, function(item){
if (item.active){
total+= item.qty * item.price;
}
});
return total;
};
return OrderFactory;
});
my new controller im trying to use
app.controller('OrderController', function($scope, OrderData) {
$scope.total = OrderData.total;
});
html snippet
<md-list-item ng-repeat="item in items.results | filter:true"
layout="row">
<span>{{ item.name }}</span>
<span flex></span>
<span>{{ item.price | currency}}</span>
<span ng-repeat="option in options.results | filter:true">{{ option.name }}</span>
</md-list-item>
<md-divider></md-divider>
<md-list-item layout="row">
<span>Order Total :</span>
<span flex></span>
<span>{{ total() | currency}}</span>
</md-list-item>
thanks for taking a look