javascript - 类型错误 : $timeout is not a function

标签 javascript angularjs angularjs-directive timeout settimeout

这里发生了什么:

我希望“取消预订”按钮在单击后超时。第一次单击时,它会更改为显示“确认取消”按钮。几秒钟后返回“取消预订”。

我的控制台给我:

TypeError: $timeout is not a function

我正在使用 AngularJS $timeout:

Controller :

'use strict';

module.controller('ReservationItemCtrl', ['$scope', '$stateParams', '$RPC',
    function($scope, $stateParams, $RPC, $timeout) {


 ......other stuff.......
 ......other stuff.......

    $scope.toggleCancelReservation = function(reservation) {
        reservation.clickedcancel = true;
        $timeout(function(){reservation.clickedcancel = false}, 4000);
    };
}
]);

模板:

  <button ng-show="!reservation.value.deleted && !deleted.value"
          class="btn btn-danger" 
          ng-show="canCancel(reservation)" ng-if="!reservation.clickedcancel" ng-click="toggleCancelReservation(reservation)">
    Cancel With Refund
  </button>
  <button type="button" class="btn btn-danger" ng-if="reservation.clickedcancel == true" ng-click="deleteReservation();" style="margin-top: -4px;">
    Confirm Cancellation
  </button>

我准确地让按钮在第一次点击时切换,如果再次点击它会正确取消/删除预订,但如果我在第一次点击后没有做任何事情,超时永远不会将其返回到原始按钮。在我的控制台中,出于某种原因,我看到 $timeout 不是一个函数?我把它包含在我的 Controller 中。我错过了什么吗?

最佳答案

您忘记了 $timeout 的内联符号:

'use strict';
module.controller('ReservationItemCtrl', ['$scope', '$stateParams', '$RPC', '$timeout',
    function ($scope, $stateParams, $RPC, $timeout) {
            ......other stuff.......
            ......other stuff.......
        $scope.toggleCancelReservation = function (reservation) {
            reservation.clickedcancel = true;
            $timeout(function () {
                reservation.clickedcancel = false
            }, 4000);
        };
    }
]);

关于javascript - 类型错误 : $timeout is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32467849/

相关文章:

javascript - AngularJS([$解析:syntax] Syntax Error: Token ':' is an unexpected token at) when value is URL

javascript - 在 AngularJS 指令中使用 SelectBoxIt

javascript - 给定项目索引,如何获得网格行/列位置

javascript - 如何在 Dojo 中创建私有(private)属性/方法?

javascript - 如何从 Controller 函数内部获取 $scope?

angularjs - 如何在 Intellij 中调试 Protractor ?

javascript - 如何防止从选定的单选按钮触发更改事件?

javascript - 三个JS | Collada 装载机纹理

html - 如何重新加载Angularjs中提交按钮的页面Onclick?

angularjs - 如何测试 AngularJS 指令