简体   繁体   English

AngularJS添加按钮微调器

[英]AngularJS Adding A Button Spinner

I have a form, when the user clicks 'check' a spinner shows to let the user know something is happening. 我有一个表单,当用户单击“检查”时,微调器将显示以让用户知道正在发生的事情。

在此处输入图片说明

I did this by just setting $scope.button = "Check"; 我只是通过设置$scope.button = "Check"; then in my controller $q.when(post_data.then( changing the text to include the animation <i class="fa fa-refresh fa-spin"> 然后在我的控制器$q.when(post_data.then(将文本更改为包括动画<i class="fa fa-refresh fa-spin">

This has many issues most important is that it's not universal and I end up doing it for every button. 这有很多问题,最重要的是它不是通用的,我最终对每个按钮都做了。

Question: Could this be done as a directive that I just amend to all my buttons, then in my controllers I could just call a start and stop process where I want to? 问题:是否可以将这作为我只是修改所有按钮的指令来完成,然后在控制器中我可以在需要的地方调用启动和停止过程? An example would be helpful. 一个例子会有所帮助。

ie

<button type="button" SPINNER ng-click="submitCheck()">Check</button>


  $scope.submitCheck = function () {
                START BUTTON SPINNER
                var post_data = post_resource($scope, CbgenRestangular);

                $q.when(post_data.then(
                    function (object) {
                      STOP BUTTON SPINNER
                        $scope.claims = object;
                    },
                    function (object) {
                        STOP BUTTON SPINNER
                        console.log(object)
                    }

                ))


            }

In the past I wrote some simple demo based on spiner.js and $timeout simulation: 过去,我基于spiner.js$timeout模拟编写了一些简单的演示:

Plunker Plunker

在此处输入图片说明

It might helpful 可能有帮助

I tried to solve exactly the same problem today. 我今天试图解决完全相同的问题。 My goal was to replace the button text with a spinning wheel and ensure the button size does not change (to avoid nasty rearrangements on the screen). 我的目标是用旋转的滚轮替换按钮文本,并确保按钮大小不变(避免在屏幕上进行不必要的重新排列)。

The result is a simple directive: https://gist.github.com/weberste/be32c7301576259e9f96 结果是一个简单的指令: https : //gist.github.com/weberste/be32c7301576259e9f96

It can be used as follows: 可以如下使用:

<button click-spinner="saveClient(client, clientForm)">
    Submit
</button>

This will replace the button text with a spinner until the promise returned by saveClient is resolved. 这将用微调框替换按钮文本,直到saveClient返回的承诺被解决。

A really nice solution can be found in the article " angularJS Button directive with busy indicator ". 一个很好的解决方案可以在文章“ 带有繁忙指示器的angularJS Button指令 ”中找到。

This solution will only require two things (it actually requires three, if you count the inclusion of spin.js, but you can use any spinner implementation that you choose): 该解决方案仅需要两件事(如果算上包含spin.js,则实际上需要三件事,但您可以使用选择的任何Spinner实现):

  1. A directive is placed on the element that should display the spinner inside of itself. 指令放置在应该在其内部显示微调器的元素上。
  2. The function that is passed to the directive needs to return a promise, which is how the directive knows when to stop the spinner. 传递给指令的函数需要返回一个promise,这就是指令知道何时停止微调器的方式。

With this method, there's no need to rely on special variables that need to be set/managed by the controller. 使用这种方法,无需依赖需要由控制器设置/管理的特殊变量。

I gave up on all smart spinner solutions in my project. 我放弃了项目中的所有智能微调器解决方案。 There is one promising about intercepting the request and responses but it does not include any client side work you might be doing before and after the request and response. 拦截请求和响应有一种希望,但是它不包括您在请求和响应之前和之后可能要做的任何客户端工作。 I have also seen directives on the button but then you need to return promises in your control methods which truly affects you project code. 我还看到了按钮上的指令,但是随后您需要在控制方法中返回真正影响项目代码的promise。 I recommend to do it the hard way, and its very flexible. 我建议以困难的方式做到这一点,并且它非常灵活。 in html: 在html中:

<button ng-click="save()" ng-disabled="isProcessing">Save</button>

in the controller 在控制器中

$scope.save = function(){
    ..do some stuff...
    $scope.isProcessing = true;
    $http.post('someapi', data).success(
        ...maybe do more
        $scope.isProcessing = false;
    );
}

There is one solution that works well you could consider this project: https://github.com/remotty/angular-ladda 有一个很好的解决方案可以考虑这个项目: https : //github.com/remotty/angular-ladda

it's build on top of ladda 它建立在ladda之上

Inside controller 内部控制器

$scope.login = function() {
    // start loading
    $scope.loginLoading = true;
    loginService.login(function() {
      // stop loading
      $scope.loginLoading = false;
    });
  } 

Inside View: 内部视图:

<button ladda="loginLoading" ng-click="login()">
  Login
</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM