0

OK, I have a controller where I need to use a function as a param

angular.module('myCtrl', function($scope) {
 $scope.$on('lines:deselectLine', function(ev, slip) {
  _.each($scope.lineItems, function(lineItem) {
    _.each(lineItem, function(lineLeague) {
      _.each(lineLeague, function(line) {
        _.each(line.rows, function(row) {              
          if (row.nss + '' === slip.nss) {
            var line = slip.line;
            if (line === row.spread.line + '') {
              row.spreadSelected = false;
            }
            if (line === row.total.line + '') {
              row.totalSelected = false;
            }
            if (line === row.moneyLineId + '') {
              row.moneyLineSelected = false;
            }
          }
        });
      });
    });
  });
 });
});

this is the full function but I need to take this part away in a function

          if (row.nss + '' === slip.nss) {
            var line = slip.line;
            if (line === row.spread.line + '') {
              row.spreadSelected = false;
            }
            if (line === row.total.line + '') {
              row.totalSelected = false;
            }
            if (line === row.moneyLineId + '') {
              row.moneyLineSelected = false;
            }
          }

so at the end I will need something like this

angular.module('myCtrl', function($scope) {
 $scope.$on('lines:deselectLine', function(ev, slip) {
  _.each($scope.lineItems, function(lineItem) {
    _.each(lineItem, function(lineLeague) {
      _.each(lineLeague, function(line) {
        _.each(line.rows, function(row, iWillBeFunctionParam) {              
           //SOMETHING NEW WILL HAPPEN HERE
        });
      });
    });
  });
 });

 $scope.iWillBeFunctionParam = function(slip) {
   if (row.nss + '' === slip.nss) {
      var line = slip.line;
      if (line === row.spread.line + '') {
         row.spreadSelected = false;
      }
      if (line === row.total.line + '') {
         row.totalSelected = false;
      }
      if (line === row.moneyLineId + '') {
         row.moneyLineSelected = false;
      }
    }
  };

});

Be aware of the $scope.$on which is an $emit...

So, what should I do to use the new function as a Param ?

2
  • $scope.iWillBeFunctionParam doesn't seem to have a closure it can reference line or row from. You will need to pass these in to it when it is invoked (add a couple parameters for them, invoke with them as arguments) Commented Jul 2, 2015 at 21:38
  • @PaulS. that is why I am here, I need to figure out how to formulate it... Commented Jul 2, 2015 at 21:39

1 Answer 1

1

It's doesn't look like you need to pass a function as a parameter to the controller, so your title is somewhat confusing.

It seems that you want to invoke some function in the innermost _.each. Perhaps I'm misunderstanding your intent, but I don't see any place where this iWillBeFunctionParam is needed to be passed as a parameter to anything.

So, you have the following - whether in a controller or not, not relevant - conceptually speaking:

var lines = [[], [], []]; // array of arrays

$scope.$on("foo", function(ev, slip){
  _.each(lines, function(line){
    _.each(line.rows, function(row){
      doSomethingWith(slip, line, row);
    })
  })
})

Then you can define your doSomethingWith function accordingly:

function doSomethingWith(slip, line, row){
   // etc...
}
Sign up to request clarification or add additional context in comments.

Comments

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.