angularjs从另一个数组填充数组

时间:2017-04-09 21:52:50

标签: javascript angularjs arrays

(angularjs非常新)通过解析csv文件尝试填充数组,因为我检查了我设法做到了,它完美地将所有内容写入$ scope.videos数组。但是当我想通过执行init()函数在页面加载上拆分该数组时,并继续将其填充到滚动上,但我只是得到这个错误,首先尝试推送数组变量。

TypeError: Cannot read property 'frame' of undefined
at init (MainController.js:33)
at new <anonymous> (MainController.js:45)
at Object.invoke (angular.js:4709)
at R.instance (angular.js:10234)
at m (angular.js:9147)
at g (angular.js:8510)
at angular.js:8390
at angular.js:1756
at m.$eval (angular.js:17444)
at m.$apply (angular.js:17544)




mvcApp.factory('parseCSV', ['$http', function($http) {
        return $http.get('http://hosting.com/videos/export_projects.csv')
            .success(function(data) {
                return data;
            })
            .error(function(err) {
                return err;
            });
    }]);

mvcApp.controller('indexController_main', ['$scope', '$sce', 'parseCSV', function($scope, $sce, parseCSV) {
    $scope.videos = [];
    parseCSV.success(function(data) {
        $scope.csv = data;
        var lines, lineNumber, data, length;
        lines = $scope.csv.split('\n');
        for(var i = 0; i < lines.length; i++) {
            data = lines[i].split(',');
            var frame = data[0];
            var title = data[1];
            $scope.videos.push({
                frame : frame,
                title : title
            });
        }
    });
    $scope.firstColumnVideos = [];
    $scope.secondColumnVideos = [];
    var init = function() {
        for(var i = 0; i < 3; i++) {
            $scope.firstColumnVideos.push({
                frame: $scope.videos[0].frame,
                title: $scope.videos[0].title
            });
            $scope.videos.splice(0, 1);
            $scope.secondColumnVideos.push({
33.             frame: $scope.videos[0].frame,
                title: $scope.videos[0].title
            });
            $scope.videos.splice(0, 1);
        }
        console.log($scope.firstColumnVideos);
    };
    init();
    $scope.loadMore = function() {
        for(var i = 0; i < 2; i++) {
            $scope.firstColumnVideos.push({
                frame: $scope.videos[0].frame,
                title: $scope.videos[0].title
            });
            $scope.videos.splice(0, 1);
            $scope.secondColumnVideos.push({
                frame: $scope.videos[0].frame,
                title: $scope.videos[0].title
            });
            $scope.videos.splice(0, 1);
        }
    };
    $scope.putIframe = function(video) {
        return $sce.trustAsHtml(video.frame);
    };
}]);

1 个答案:

答案 0 :(得分:0)

parseCSV.success是异步调用,因此您无法立即获取数据。您必须将init()移至parseCSV的成功回调。

parseCSV.success(function(data) {
        $scope.csv = data;
        var lines, lineNumber, data, length;
        lines = $scope.csv.split('\n');
        for(var i = 0; i < lines.length; i++) {
            data = lines[i].split(',');
            var frame = data[0];
            var title = data[1];
            $scope.videos.push({
                frame : frame,
                title : title
            });
        }
        init();   // do the push here.
    });