Sunday, September 24, 2017

Why would "this" in function inside angularjs service be undefined

Why would this inside function be undefined in angularjs service?

(function(){
    'use strict';

    angular.module('myServices').service('SliceService', function () {

        var Slice = function(intervals, intervalSpan) {
            this.activeSlice = [];
            this.hasNext = true;
            this.hasPrevious = false;
            this.intervals = intervals;
            this.intervalSpan = intervalSpan;
        }

        return Slice;
    });

}());

In the above example when using it in a controller, this.activeSlice throws an error TypeError: Cannot set property ‘activeSlice’ of undefined.

Source: AngularJS



from Angular Questions https://angularquestions.com/2017/09/25/why-would-this-in-function-inside-angularjs-service-be-undefined/
via @lzomedia #developer #freelance #web

No comments:

Post a Comment