遵循this教程,使用angular实现AWS sdk之后,我从jshint(使用grunt服务该应用程序)获取了AWS is not defined

我用bower install aws-sdk-js --save安装了sdk,它正确显示在我的index.html文件中。

这是我的控制器:

angular.module('myApp')
.controller('S3uploadCtrl', function ($scope) {

  console.log(AWS);
  $scope.creds = {
    bucket: 'myBucket',
    accessKey: 'accKey',
    secretKey: 'secKey'
  };

  $scope.upload = function() {
    // Configure The S3 Object
    AWS.config.update({ accessKeyId: $scope.creds.accessKey, secretAccessKey: $scope.creds.secretKey });
    AWS.config.region = 'us-west-2';
    var bucket = new AWS.S3({ params: { Bucket: $scope.creds.bucket } });

    if($scope.file) {
      var params = { Key: $scope.file.name, ContentType: $scope.file.type, Body: $scope.file, ServerSideEncryption: 'AES256' };

      bucket.putObject(params, function(err, data) {
        if(err) {
          // There Was An Error With Your S3 Config
          alert(err.message);
          return false;
        }
        else {
          // Success!
          alert('Upload Done');
        }
      })
      .on('httpUploadProgress',function(progress) {
        // Log Progress Information
        console.log(Math.round(progress.loaded / progress.total * 100) + '% done');
      });
    }
    else {
      // No File Selected
      alert('No File Selected');
    }
  };
  function alert(msg) {
    console.alert(msg);
  }
});


谷歌上没有太多关于此的内容。我发现了另外一个SO question,但我一直没有遵循。 (更改了<script>标签的顺序等)

最佳答案

这是一个JSHint错误。 JSHint确保您正在访问已定义的变量,并且不知道运行时存在AWS全局变量。因此,您需要告诉JSHint该globa变量存在,并且允许您的代码访问该全局变量(尽管您可能应该将其隐藏在角度服务后面,以使您的代码可测试)。

编辑您的.jshintrc文件(它可能具有另一个名称:检查您的构建配置),然后添加(或修改)以下规则:

"globals": { "AWS" : false }

07-25 23:12
查看更多