Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何从1个控制器启动倒计时计时器,并以其他角度显示_Angularjs - Fatal编程技术网

Angularjs 如何从1个控制器启动倒计时计时器,并以其他角度显示

Angularjs 如何从1个控制器启动倒计时计时器,并以其他角度显示,angularjs,Angularjs,这段代码中的计时器是在程序启动时启动的,当我们在第二个控制器中设置静态值时也是如此 function readycntrl($scope,$http, 'mySharedService') { 当按下第一个控制器(readycntrl)中的按钮时,计时器的值是动态的,从php页面(timer.ajax)获取,并显示在计时器标记中的第二个控制器(questcntrl)中,有人能建议我如何启动计时器吗 这是服务 var demoModule = angular.module('demoModul

这段代码中的计时器是在程序启动时启动的,当我们在第二个控制器中设置静态值时也是如此

function readycntrl($scope,$http, 'mySharedService') { 
当按下第一个控制器(readycntrl)中的按钮时,计时器的值是动态的,从php页面(timer.ajax)获取,并显示在计时器标记中的第二个控制器(questcntrl)中,有人能建议我如何启动计时器吗

这是服务

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

demoModule.factory('mySharedService', function($rootScope) {
    var sharedService = {};

    sharedService.sharedmessage  = '';

    sharedService.prepForPublish = function(response) {//get the value
        this.sharedmessage  = response;
        this.publishItem();
    };

    sharedService.publishItem = function() {           //$broadcast the value
        $rootScope.$broadcast('handlePublish');
    };

    return sharedService;
});
现在这些是控制器

//controller which sends response to service
function ready cntrl($scope,$http,sharedService) {
    $scope.readyclick = function() {
        $scope.current= 'x';
        $scope.total= '';
        $scope.inpu= '';
        alert($scope.current);
        $http({
            method : "post",
            url    : 'fourth-submit-ajax.php',
            data   : $.param({'current':$scope.current ,
                              'total'  :$scope.total,
                              'input'  :$scope.input}),
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).success(function(response){
            $scope.ga=response;
            sharedService.prepForPublish(response);
        });
    }
}
当按下
readyclick
按钮(
readycntrl
controller)时,计时器应在
questcntrl
controller中启动,并且不应通过任何其他方式启动

//second controller which accepts it
function questcntrl($scope,sharedService) {
    $scope.$on('handlePublish', function() {
        $scope.sharedmessage=sharedService.sharedmessage ;
    });

    $scope.startTimer = function (){
        $http.get('timer-ajax.php').success(function(response) {
            // this is the value of timer dynamically coming from timer.php
            $scope.wow = response[0].time;
        }); 
        $scope.$broadcast('timer-set-countdown');
        $scope.$broadcast('timer-start');

        $scope.timerRunning = true;
    };

    $scope.stopTimer = function (){
        $scope.$broadcast('timer-stop');
        $scope.timerRunning = false;
    };

    $scope.$on('timer-stopped', function (event, data){
        console.log('Timer Stopped - data = ', data);
    });
}
现在,这些是服务的依赖注入

readycntrl.$inject = ['$scope', 'mySharedService'];        
questcntrl.$inject = ['$scope', 'mySharedService'];
html代码:

<div data-role="main" class="ui-content">
    <div ng-controller="readycntrl">
       <table align="center">
            <tr>
                <td><p>Are You Ready ?</p></td>
            </tr>
            <tr>
                <td><a href="" data-transition="flow" class="…" ng-click="readyclick()">start</a></td>
                <td><a href="#secall" data-transition="turn" class="…">back</a></td>
            </tr>
        </table>
    </div>
</div>

你准备好了吗

倒计时值是questcnrtl控制器中存在的变量(wow)

<div ng-controller="questcntrl">
    {{sharedmessage}}
    <timer countdown="wow" interval="1000" finish-callback="askstart(current,total,inpu); startTimer();">{{countdown}}{{}}</timer>
</div>

{{sharedmessage}}
{{倒计时}{}{}
timer-ajax.php仅包含值30(动态来自php) 还有另一个timer.js文件用于此,这是常见的


有人请帮帮我……

为了在控制器中使用该服务,您必须将其引用到控制器中

function readycntrl($scope,$http, 'mySharedService') { 

谢谢@MiTa现在我有了问题的解决方案

我们需要将$http添加到依赖项注入部分来解决这个问题

readycntrl.$inject = ['$scope','$http','mySharedService'];
将此添加到代码中可以解决问题 再次感谢@MiTa
谢谢大家

HTML似乎已经崩溃了,您从未关闭表并在行之间创建div。为了便于查看,我应该最小化代码吗?应该是:
readycntrl.$inject=['$scope','$http''mySharedService']
感谢@MiTa解决了这个问题。在
readycntrl
中,您似乎将
$scope.inpu
传递给
第四个提交ajax.php
。请检查t是在复制粘贴时丢失的,还是您的代码有误。在
questcntrl::startTimer
中,请检查
response
是否实际上是一个JSON对象,并且
vava
值设置是否正确。另外,到目前为止,请使用您的代码创建一个社区,以便社区有一个更好的起点。我也看不到你在哪里定义了
计时器
方向当我们把sharedService和$http放在一起时,http部分不起作用。很抱歉我修改了这个问题,因为代码太大了