【问题标题】:AngluarJS $timeout not workingAngularJS $超时不工作
【发布时间】:2017-11-12 12:41:35
【问题描述】:

我正在尝试在我的视图中创建延迟操作。
单击按钮时,会出现相应的消息,但是我希望该消息在 2000 毫秒内消失。
我发现了一些推荐 $timeout 的情况,并且我似乎遵循了这些情况下的解决方案,但是 $timeout 函数不会更改标签文本。

为什么我的$timeout 在这种情况下不起作用?

感谢您就此事提供任何指导。请在下面找到我的代码。

// app.js

var app = angular.module('myApp', []);

// mainController.js

app.controller("mainController", ["$scope", function($scope, $timeout) {
  $scope.fireTrigger = function(str) {
    $scope.triggeredValue = (str) + " fired";
    $timeout(function() { 
            $scope.triggeredValue = "";
        }, 2000);
  }
}]);

// index.html

<!DOCTYPE html>
<html ng-app="myApp">

<head>
<title>myApp</title>
<link rel="stylesheet" 
href="entertainment.css">
</head>

<body ng-controller="mainController">
    <h3 ng-bind="triggeredValue"></h3>
// ...

【问题讨论】:

    标签: javascript angularjs timeout


    【解决方案1】:

    在内联数组中注入依赖,然后在控制器的工厂函数中使用它

    app.controller("mainController", ["$scope", "$timeout", //<-- add $timeout dependency
       function($scope, $timeout) {
    

    【讨论】:

    • 谢谢你是对的,我还注意到我忘记在我的 onclick 函数中调用函数本身。
    猜你喜欢
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 2012-08-13
    • 2017-06-19
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多