2015-06-12 21 views
5

Tôi cố gắng sử dụng angular uploader để tải xuống bằng npm và sau đó duyệt tất cả trong một tệp. Bạn có thể xem chi tiết lỗi hereLỗi: [ng: areq] Đối số 'mô-đun' không phải là một hàm, có đối tượng

Tôi không thể hiểu được lỗi vì hiện tại tôi vẫn còn mới với AngularJS. Đây là mã của tôi

var angular = require('angular'); 
angular 
    .module('jobApp', [require('angular-material'), require('ng-file-upload')]) 
    .controller('MainCtrl', ['$rootScope', '$scope', '$mdToast', '$animate', '$http', '$timeout', '$q', '$log', 'Upload', 
     function($rootScope, $scope, $mdToast, $animate, $http, $timeout, $q, $log, Upload) { 
     'use strict'; 
     // Initialize the scope variables 
     $scope.$watch('files', function() { 
      $scope.upload($scope.files); 
     }); 
     $scope.upload = function (files) { 
      if (files && files.length) { 
       for (var i = 0; i < files.length; i++) { 
        var file = files[i]; 
        Upload.upload({ 
         url: 'http://sr-recruit.herokuapp.com/resumes', 
         fields: { 
          'name': $scope.name, 
          'email': $scope.email, 
          'about': $scope.about 
         }, 
         file: file 
        }).progress(function (evt) { 
         var progressPercentage = parseInt(100.0 * evt.loaded/evt.total); 
         $scope.log = 'progress: ' + progressPercentage + '% ' + 
            evt.config.file.name + '\n' + $scope.log; 
        }).success(function (data, status, headers, config) { 
         $scope.log = 'file ' + config.file.name + 'uploaded. Response: ' + JSON.stringify(data) + '\n' + $scope.log; 
         $scope.$apply(); 
        }); 
       } 
      } 
     }; 
    }]); 

CẬP NHẬT

Bây giờ đang sử dụng Browserify-shim để kèm theo đúng góc-file-upload ở định dạng Commonjs. Cảm ơn @ryan Johnson.

Đây là package.json

{ 
    "browser": { 
     "angular": "./node_modules/angular/angular.js", 
     "ng-file-upload": "./node_modules/ng-file-upload/dist/ng-file-upload-all.js" 
    }, 
    "browserify-shim": { 
     "angular": { 
      "exports": "angular" 
     },  
     "ng-file-upload": { 
      "exports": "angular.module('ngFileUpload').name" 
     } 
    }, 
    "browserify": { 
     "transform": [ 
      "browserify-shim" 
     ] 
    } 
} 

tôi vẫn lỗi

TypeError: Cannot read property '' of undefined 

Không chắc lý do tại sao. FYI Tôi sử dụng Laravel-elixir được xây dựng trong trình duyệt để chạy tác vụ.

+0

Mảng * require * (2 arg đến 'mô-đun') phải là một chuỗi các chuỗi – Phil

+0

ý của bạn là gì? '.module ('jobApp', [require ('angular-material'), yêu cầu ('ng-file-upload')])'? – Muhaimin

+0

Nó phải là '.module ('jobApp', ['ngMaterial', 'ngFileUpload'])'. Tôi không chắc chắn những gì các phiên bản mô-đun nút của những người xuất khẩu nhưng tôi đặt cược nó không phải là một chuỗi (chỉ cần kiểm tra ng-file-upload và nó không có xuất khẩu mô-đun) – Phil

Trả lời

15

Tôi nhận thấy rằng bạn đang sử dụng require('ng-file-upload') để bao gồm mô-đun. Mặc dù mô-đun ng-file-upload có sẵn thông qua npm các tệp JS đã tải xuống không ở định dạng phổ biếnJS.

Để cho require('ng-file-upload') để làm việc trong ví dụ của bạn tập tin nguồn sẽ cần phải xuất mô-đun như vậy:

module.exports = angular.module('ng-file-upload').name; 

Vì đây không phải là trường hợp của tập tin bạn đang bao gồm cả bạn cần phải shim nó để làm việc với Browserify. Đây là một bài viết hay trên Properly shim AngularJS applications using Browserify.

+0

FYI, tôi sử dụng chức năng trình duyệt laravel-elixir. bởi vì tôi nghĩ rằng nó sẽ tự động trình duyệt nó quá. Tôi sử dụng npm để quản lý các angularjs và tôi không tìm thấy nó có vấn đề để trình duyệt nó – Muhaimin

+0

@MuhaiminAbdul đó là vì các mô-đun nút góc có các tệp 'index.js' với những thứ như' module.exports = 'ngMaterial'' – Phil

+0

Tôi đã cố gắng shim nó nhưng không may mắn vì nó hóa ra là lỗi khác 'TypeError: Không thể đọc thuộc tính '' của undefined' – Muhaimin

Các vấn đề liên quan