AngularJS ng-show和$ timeout不起作用 [英] AngularJS ng-show and $timeout not working

查看:146
本文介绍了AngularJS ng-show和$ timeout不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是AngularJS的新手,并且正在使用ng-show和if else语句(带有$ timeout)在AngularJS中做一个单独的项目.但是,显示几秒钟后,我无法获得超时的答案.我从当前处理此主题的stackoverflow页面中尝试了这种方法的组合,但没有进行任何操作.除超时功能外,其他所有功能均有效.

I am new to AngularJS and am doing an individual project in AngularJS using ng-show and if else statement with $timeout. However I cant get the the answers to timeout after a few seconds of being displayed. I tried may of combinations of this from current stackoverflow pages dealing with this subject and nothing. Everything else works but the timeout ability.

--HTML----
       <form>
          <div class="form-group">
            <label>{{ question }}</label>
            <input type="text" class="form-control" ng-model="myAnswer"><!--angular directive-->
          </div>

        </form>
            <div ng-show="myquestion(myAnswer)"> <!--angular directive-->
                <h5>{{ ansConf }}</h5><!-- if answer equals blue whale return string-->
            </div>
            <div ng-hide="myquestion(myAnswer)">
                <h5>{{ wrongAns }}</h5>
            </div>
            </div>
            <div class="col-md-4 odb" ng-init="whaleOne = 'assets/img/site/2560 8.jpg'" >
                <a href="#" ng-click="infoOne()"> <!--angular directive-->
                <img class="img-thumbnail img-responsive" ng-src="{{ whaleOne }}"> <!--angular directive-->
            </a>
            </div>

--- AngularJS Code ---

var app = angular.module("bigBlue", []); /* New module called gamesite */

app.controller('whaleController', ['$scope', '$window', function($scope, $window, $timeout) { 

  $scope.question = 'Which whale is blue ?';
  $scope.rightAns = 'blue whale';
  $scope.wrongAns = 'nope';
  $scope.ansConf = 'Blue Whale is correct !';

  // setting value of the first question variable '' open string
  $scope.myAnswer = '';

  $scope.myquestion = function(myAnswer){
    if (myAnswer == $scope.rightAns){
      return true;
      $timeout(function () {
                $scope.myAnswer = false;
            }, 3000);
    }
    else if (myAnswer != $scope.rightAns){
      return false;
    }

  }

推荐答案

中断功能

函数立即在调用return的点停止.

A function immediately stops at the point where return is called.

返回后,您永远不会调用超时,请最后移动它

After return you timeout is never invoking, move it in the end

  if (myAnswer == $scope.rightAns){

      $timeout(function () {
                $scope.myAnswer = false;
            }, 3000);
     return true;
    }

这篇关于AngularJS ng-show和$ timeout不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆