angularjs - Passing JavaScript to Closure in JavaScript -


i'm learning how use javascript. i've run situation i'm getting error. error is: typeerror: 'undefined' not object (evaluating 'this.flagged'). i've narrowed down code happening. code looks this:

var flagged = false; var intervals = [];  return {   flagged: flagged,   intervals: intervals,    createinterval : function (options) {     var defer = $q.defer();     if (this.throwserror) {       defer.reject('there error creating interval.');     } else {       this.intervals.push(         $interval(function() {           console.log('here 1');           console.log(this.flagged);          },         1000       ));     }   } }; 

the error gets thrown @ the: console.log(this.flagged); i'm guessing has fact "this" isn't visible. yet, if "this" isn't visible, i'm not sure how value flagged. can please explain me need value flagged?

thank you!

when using this inside $interval won't pointing original object, however, can this:

var flagged = false; var intervals = [];  return {   flagged: flagged,   intervals: intervals,    createinterval : function (options) {     var defer = $q.defer(),         self = this;     if (this.throwserror) {       defer.reject('there error creating interval.');     } else {       this.intervals.push(         $interval(function() {           console.log('here 1');           console.log(self.flagged);          },         1000       ));     }   } }; 

notice var self = this;


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -