1

When I call a factory method within the controller it's fine- but when I call a factory method within a controller method it's undefined.

Here's my factory:

app.factory('config', ['$http', '$interval', function($http, $interval, $scope) {
    return {
        lines: function() {
            return $http({
                url: appPath+'/config.php?data=lines',
                method: 'JSON'
            })
        },
        updateLines: function() {
            $http.post(appPath+'/write.php?handler=update line', $scope.lines).
                success(function(data, status, headers, config) {
                    return true;
                }).error(function(data, status, headers, config) {
                    return false;
                });
        }
    }
}]);

My Controller:

app.controller('lineController', function($scope, $interval, $http, config) {
    // this works fine
    config.lines().success(function(data) {
        $scope.lines=data;
    });

    this.addToLine = function(line, customer) {

    };

    this.unassign = function(customer, line) {
        var index = line.indexOf(customer);
        line.splice(index, 1);

        // Cannot read property 'lines' of undefined
        config.updateLines();
    };
});

Inside the controller scope my factory config is defined, however when referencing config in a method the factory is undefined. AngularJs is new to me, so if this is bad practice then I'd appreciate being pointed in the right direction.

Thanks

4
  • Did you include the file with this factory in the app? This factory is in the same module? Commented Dec 12, 2014 at 19:24
  • Yes they are in the same module. Commented Dec 12, 2014 at 19:25
  • Are you getting any exception at the console? Commented Dec 12, 2014 at 19:26
  • No, I get "Cannot read property 'lines' of undefined" in the console. The factory works fine when I call it outside of a method though. Commented Dec 12, 2014 at 19:29

1 Answer 1

3

The error message you are getting appears to be correct. $scope.lines does not exist in your factory but in your controller. So what you might have intended to do would be to pass that value in from the controller to the factory like the below

function in controller

config.updateLines($scope.lines);

function in factory

updateLines: function(lines) {
    $http.post(appPath+'/write.php?handler=update line', lines).
        success(function(data, status, headers, config) {
            return true;
        }).error(function(data, status, headers, config) {
            return false;
        });
}
Sign up to request clarification or add additional context in comments.

1 Comment

As a rule of thumb, $scope should only be injected into controllers. The way to think about it is services and factories are for your data, controllers and $scope are for your UI. The controller's job is to orchestrate data living on the $scope and do interesting things with it, such as passing it to services.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.