javascript - 如何在 Angular 上使用 Ajax

标签 javascript jquery ajax angularjs

我想在服务器上上传文件并有进度条。我是用ajax上传的。现在,我想将它们转换为 AngularJS。如何在 angularjs 上使用 ajax。我正在研究 ngularjs。请帮助我。谢谢 HTML:

<form action="uploadFile" method="post" enctype="multipart/form-data">
        <input type="file" name="file" multiple><br>
        <input type="submit" value="Upload File to Server">
    </form>

    <div class="progress">
        <div class="bar"></div >
        <div class="percent">0%</div >
    </div>

    <div id="status"></div>

和js文件:

<script>
(function() {

var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');

$('form').ajaxForm({
    beforeSend: function() {
        status.empty();
        var percentVal = '0%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    uploadProgress: function(event, position, total, percentComplete) {
        var percentVal = percentComplete + '%';
        bar.width(percentVal)
        percent.html(percentVal);
        //console.log(percentVal, position, total);
    },
    success: function(xhr) {
        var percentVal = '100%';
        bar.width(percentVal)
        percent.html(percentVal);

    },
    complete: function(xhr) {
        status.html(xhr.responseText);
    }
}); 

})();       
</script>

最佳答案

使用$http .

该链接的示例:

$http.get('/someUrl').
  success(function(data, status, headers, config) {
    // this callback will be called asynchronously
    // when the response is available
  }).
  error(function(data, status, headers, config) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

至于显示进度,这个插件看起来很有前途:Angular Loading Bar .

即使只是包含它之后它仍然可以工作。

angular.module('myApp', ['angular-loading-bar'])

关于javascript - 如何在 Angular 上使用 Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31471221/

相关文章:

javascript - 发布没有 <form> 标签的表单有什么缺点吗

javascript - 如何使用 Ajax 调用 JavaScript 文件并使用该 JavaScript 文件中的逻辑

javascript - j_security_check 使用 ajax

javascript - `>>>` 在 JavaScript 中意味着什么?

javascript - Angularjs - 将指令中的值绑定(bind)到 ng-show/hide 文档中的任何位置

javascript - 关闭结帐回调参数可用吗?

javascript - Materialize 中的 OnSelect Datepicker 在两个输入之间设置最大日期 + 9

javascript - this._mouseInit() 不是一个函数

javascript - jQuery sort 和 appendTo 在 Internet Explorer 和 Safari 中不起作用

php - 提交表单并在同一页面上显示结果而无需刷新