Angularjs解析postPromise没有运行

时间:2015-08-25 11:44:53

标签: angularjs angular-ui-router

我目前正在研究MEAN堆栈webapp,而postPromise似乎并没有起作用。我试图从我的数据库中提取数据并将其下载为CSV。稍微调试一下后,我把它缩小到postPromise没有被调用(console.log('看马!我跑了');没有运行。)我有相同的代码在应用程序的其他地方正常运行,所以我有点难过。

UI-路由器:

.state('adminDownload', {
            url: '/download',
            templateUrl: '../../views/admin-download.html',
            resolve: {
                postPromise: ['users', function(users){
                    console.log('look, ma! I ran!'); //is not called
                    return users.getAll();
                }]
            },
            onEnter: ['$state', 'auth', function($state, auth){
                if( ! auth.isLoggedIn()){
                  $state.go('adminLogin');
                }
            }]
        });

用户工厂:

    routerApp.factory('users', ['$http', function($http){
        var o = {
            users: []
        };

        o.getAll = function() {
            console.log('getAll was called');
            return $http.get('/users').success(function(data){
                angular.copy(data, o.users);
            });
        };

        o.create = function(user) {
            return $http.post('/users', user).success(function(data){
                o.users.push(data);
            });
        };

        return o;
    }]);

index.js, GET users:

/*GET users*/
router.get('/users', function(req, res, next) {
    User.find(function(err, users) {
        if(err) {
          return next(err);
        }

        console.log('get users called');//is not called
        res.json(users);

    });
});

CSV转换代码,实际上是问题为了调试目的,已经注释了很多东西

routerApp.controller('AdminCtrl', ['$scope', 'users', 'questions', function ($scope, users, questions){
    $scope.getData = function(){

        //get the results from user objects
        var data = [];

        console.log(users.users); //returns '[]'

        for (var i = 0; i < users.users.length; i++) {
            console.log('the loop got called');// is not called at users.users.length === 0
            console.log(users.users[i].results);
            $scope.data.push(users.users[i].results);
        }

        console.log('not in the loop any more :('); //is called

        // Convert Object to JSON
        var jsonObject = JSON.stringify(data);

        var convertToCSV = function(objArray) {
            var str = Papa.unparse(objArray);
            return str;
        }

        //console.log($scope.data);
        console.log(jsonObject); //returns '[]'
        //console.log(convertToCSV(jsonObject));

        //download .csv
        //window.open( "data:text/csv;charset=utf-8," + escape(convertToCSV(jsonObject)));

    };
}]);

公共/视图/管理-greeting.html:

<div ng-app = 'routerApp', ng-controller = 'AdminCtrl'>
    <h1>Hello, Friend</h1>
    <button ng-click = 'getData()' href = 'data' download>Download Data</button>
</div>
<div ng-app = 'routerApp', ng-controller = 'AuthCtrl'>
    <button ng-click = 'logout()'>Logout</button>
</div>

1 个答案:

答案 0 :(得分:0)

请在状态定义中添加控制器

    .state('adminDownload', {
        url: '/download',
        templateUrl: '../../views/admin-download.html',
        controller: 'AdminCtrl',
        resolve: {
            postPromise: ['users', function(users){
                console.log('look, ma! I ran!'); //is not called
                return users.getAll();
            }]
        },
        onEnter: ['$state', 'auth', function($state, auth){
            if( ! auth.isLoggedIn()){
              $state.go('adminLogin');
            }
        }]
    });