angularjs资源错误a.push不是函数

时间:2016-04-14 12:32:42

标签: javascript angularjs json

我无法通过json值保存上传的文件名和格式。我收到回复但我无法在json文件sample_data.json中保存和更新。它显示错误:

  

TypeError:a.push不是函数

请帮帮我。

错误:

stacktrace

提前致谢。

HTML

    <!DOCTYPE html >
    <html ng-app='uploadfiles'>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title>Upload files</title>
    </head>

    <body ng-controller="uploadCtrl">
    <div class="container" >
        <form >
        <input type="file" ng-file-model="files" multiple  model="model" />
        <button type="button" ng-click="upload()">Upload</button>
    </form>
        <p ng-repeat="file in files">
          {{file.name}}
        </p>
        <div id="result">
            <p>{{msg}}</p>
        </div>
    </div>
    <script src="lib/angular.min.js" type="text/javascript"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular-resource.min.js"></script>
    <script src="app/app.js" type="text/javascript"></script>
    <script src="app/controller.js" type="text/javascript"></script>
    <script src="app/directives.js" type="text/javascript"></script>
    </body>
    </html>

app.js

    var uploadApp = angular.module('uploadfiles',['ngResource',  'myAppServices']);

controller.js

    uploadApp.controller('uploadCtrl',['$scope','uploaddata', function($scope, uploaddata, $http) {
      $scope.files =[];

      $scope.upload=function(){   
         uploaddata.save($scope.files, function(data) {
            $scope.msg ='file saved';        
          });

      };
    }]);

    var myAppServices = angular.module('myAppServices', ['ngResource']);

    myAppServices.factory('uploaddata', ['$resource',
      function($resource) {
        return $resource('../uploadfiles/temp/sample_data.json', {}, {});
      }
    ]);

Directive.js

    uploadApp.directive('ngFileModel', ['$parse', function ($parse) {
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                var model = $parse(attrs.ngFileModel);
                var isMultiple = attrs.multiple;
                var modelSetter = model.assign;
                element.bind('change', function () {
                    var values = [];
                    angular.forEach(element[0].files, function (item) {
                        var value = {
                           // File Name 
                            name: item.name,
                            //File Size 
                            size: item.size,
                            type: item.type,
                            //File URL to view 
                            url: URL.createObjectURL(item),
                            // File Input Value 
                           _file: item
                        };
                        values.push(value);
                    });
                    scope.$apply(function () {
                        if (isMultiple) {
                            modelSetter(scope, values);
                        } else {
                            modelSetter(scope, values[0]);
                        }
                    });
                });
            }
        };
    }]);

0 个答案:

没有答案