0

I'm trying to make an AJAX call with ngResource, In the code below 'a' and 'b' both print, but the AJAX call from Table.import() does not get made. If I move the AJAX call outside of onFileRead, then it works. What could be the problem?

var TableImportController = ['$scope','Table', 'project', 'table',
    function($scope, Table, project, table) {

    $scope.table = table;
    $scope.project = project;

    $scope.onFileRead = function(file) {

        console.log('a');
        Table.import({ data : file.data}, function() {

        }, function() {

        });
        console.log('b');
    };

}];

Where Table is an ngResource

.factory('Table', function($resource) {
        var Table = $resource('/api/tables/:id:listAction/:itemAction',
            {
                id: '@id',
                listAction: '@listAction',
                itemAction: '@itemAction'
            },
            {
                update: { method: 'PUT' },
                import : { method: 'POST', params: { listAction: 'import' }},

            }
        );

        return Table;
});
mmattax
  • 27,172
  • 41
  • 116
  • 149

2 Answers2

0

You are declaring $scope.onFileRead as a function. What is calling onFileRead? When you move the call outside of the function, it is being run as part of initialization. What provides the input file? Probably bind to the onFileRead function from something in your DOM.

aet
  • 7,192
  • 3
  • 27
  • 25
  • onFileRead is a callback that is in a custom directive (isolated scope, param is '&'). $scope.onFileRead() is being called properly. – mmattax Jul 26 '13 at 11:18
0

I figured it out. It looks like I ran into this bug: https://github.com/angular/angular.js/issues/2794#issuecomment-18807158.

I solved the issue by wrapping the AJAX call (and eventually moved it to where the onFileRead callback is triggered) in a scope.$apply(function() { });

mmattax
  • 27,172
  • 41
  • 116
  • 149