Non
Non

Reputation: 8589

Using function as a param in a controller

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 ?

Upvotes: 0

Views: 61

Answers (1)

New Dev
New Dev

Reputation: 49590

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...
}

Upvotes: 1

Related Questions