While this question leans towards a JavaScript issue rather than Angular, the context remains within Angular. The problem arises when attempting to call the doSomething
method from lodash's ._forEach
, which creates its own scope causing an inability to access var service
using this
as it refers to the loop itself.
So, what would be the most effective approach for invoking doSomething
from within the code snippet below (inside the forEach
)?
'use strict';
angular.module('app').service('fooService', function()
{
var service = {
barMethod: function(arrayOfObjects){
//this.doSomething(); - this works if not commented out,
//but I need it from within loop
_.forEach(arrayOfObjects, function (ob) {
this.doSomething();
});
},
doSomething: function(){
//do something
},
};
return service;
});