1

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

2 Answers 2

1

Ok the problem was

<span>{{ total(items) | currency}}</span>

needed to be changed to

<span>{{ total(items.results) | currency}}</span>
Sign up to request clarification or add additional context in comments.

Comments

1

You total method should accept item parameter & from UI you should pass items.results object so that service can iterate that array.

Code

OrderFactory.total = function(items) {
    var total = 0;
    angular.forEach(items, function(item) {
        if (item.active) {
            total += item.qty * item.price;
        }
    });
    return total;
};

Markup

<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(items.results) | currency}}</span>
</md-list-item>

2 Comments

@ChrisG there was mistak of item & items..could you try updated answer
thanks i have answer below... the data was held in items.results

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.