TypeError: $timeout不是函数



这里发生了什么:

我想"取消预订"按钮有一个超时后,它的点击。在第一次点击它改变显示"确认取消"按钮。几秒钟后,它返回到"取消预订"。

我的控制台给我:

TypeError: $timeout is not a function

我使用AngularJS $timeout:

控制器:

'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 is not a function出于某种原因?我把它包含在我的控制器中。我错过什么了吗?

您忘记了$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);
        };
    }
]);

你必须在控制器依赖项中包含$timeout

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

相关内容

  • 没有找到相关文章

最新更新