user1189352 user1189352 - 5 months ago 9
AngularJS Question

How can I access a constructor member within a constructor function?

Sorry I'm not sure how to word the question.. I know it's some sort of scope issue.. but is what I'm trying to accomplish not possible?

app.factory('SystemStatusConnectionFactory', ['$timeout', '$q', 'SystemStatusFactory', 'SystemStatusOptionsFactory',
function ($timeout, $q, SystemStatusFactory, SystemStatusOptionsFactory) {

var self = this;

var SystemStatusConnectionFactory = function (ip, user, pw, options) {
this.data = {
count: 0
};

this.PollIP = function () {
console.log(this.data.count); //WORKS
$timeout(function () {
console.log(self.data.count); //DOES NOT WORK
console.log(this.data.count); //DOES NOT WORK
}, 1000);
}
};
... etc

Dan Dan
Answer

Def a scoping issue. It's hard know exactly whats going on without seeing more of your code, but at first glance... See my inline comments in first block.

app.factory('SystemStatusConnectionFactory', ['$timeout', '$q', 'SystemStatusFactory', 'SystemStatusOptionsFactory',
function ($timeout, $q, SystemStatusFactory, SystemStatusOptionsFactory) {

    var self = this; // this is assigning self to the callback function your in.

    var SystemStatusConnectionFactory = function (ip, user, pw, options) {

        this.data = {  // this here represents this current function 'SystemStatusConnectionFactory'.
            count: 0
        };

        this.PollIP = function () { // same here
            $timeout(function () {
                console.log(this.data.count);  // This represents the callback function of the timeout.
            }, 1000);
        }
    };
}

if your bind this with your functions it will go a long way to fixing ur problems.

app.factory('SystemStatusConnectionFactory', ['$timeout', '$q', 'SystemStatusFactory', 'SystemStatusOptionsFactory',
(function ($timeout, $q, SystemStatusFactory, SystemStatusOptionsFactory) {

    var self = this; //this is now pointing to 'app'

    var SystemStatusConnectionFactory = (function (ip, user, pw, options) {

        this.data = {  // this is now pointing to app as well.
            count: 0
        };

        this.PollIP = function () {
            $timeout(function () {
                console.log(self.data.count);  Self points to app.
                console.log(this.data.count);  // This represents the callback function of the timeout.
            }, 1000);
        }
    }).bind(this);
}).bind(app) // Use bind to set app as the context of the function or 'this'

** I am making the assumption you want 'this' to refer to app within the callback. **