TypeError:$ timeout不是函数

时间:2022-12-15 01:17:57

What is happening here:

这里发生了什么:

I want the "cancel reservation" button to have a timeout on it after its clicked. On first click it changes to show "confirm cancellation" button. After a few seconds it return back to "cancel reservation".

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

My console gives me:

我的控制台给了我:

TypeError: $timeout is not a function

I am using AngularJS $timeout:

我正在使用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);
    };
}
]);

template:

  <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>

I am accurately getting the button to switch when first clicked and then if clicked again it correctly cancels/deletes the reservation but if I don't do anything after the first click the timeout never returns it back to the original button. In my console I see that $timeout is not a function for some reason? I have it included in my controller. Am I missing something?

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

2 个解决方案

#1


7  

You forgot the inline notation of $timeout:

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

#2


2  

You have to include $timeout in the controller dependencies

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

'use strict';

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

#1


7  

You forgot the inline notation of $timeout:

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

#2


2  

You have to include $timeout in the controller dependencies

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

'use strict';

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