简体   繁体   English

时间间隔后在Angular JS中调用函数

[英]Call a function after a time gap In Angular JS

I am trying to refresh a page after showing a successfull message. 我在显示成功消息后尝试刷新页面。 I tied with many snippets but either it gets refreshed without showing message or vice-versa. 我绑了很多代码片段,但它们要么刷新却不显示消息,反之亦然。

So I thought of Calling the Refresh function after an interval or Trigger again the button click event after some time to get the page refreshed. 因此,我想到了在间隔后调用Refresh函数或在一段时间后再次触发按钮单击事件以刷新页面。

Here is the code, How to achieve the above scenario? 这是代码,如何实现以上方案?

JS CODE JS代码

$scope.Save = function (data) {
    debugger;
    $http.post($rootScope.WebApiURL + '/updatemanifeststatus');
    var value= $http.get($rootScope.WebApiURL + '/getmanifeststatus' );

        $scope.manifeststatus = data;

        $scope.showstatus = true;
        $scope.alert = { type: 'success', msg: 'Published Successfully.' };
        $(".statusDivPublish").show();

        $scope.Refresh1();

    }

    $scope.Refresh1 = function () {
        //refresh
        $state.go($state.current, {}, { reload: true });
    }
});

HTML Button Call HTML按钮调用

<div class="col-sm-offset-1">
    <button id="PublishButton" class="btn btn-default shiny " ng-disabled="manifeststatus.enablePublishButton" ng-click="Save(manifeststatus)">Publish</button>
</div>

What I want to do Here is Call the Refresh1() function after a time gap in such a way, the page gets refreshed after showing the message. 我要在这里做的是在这样的时间间隔后调用Refresh1()函数,该页面在显示消息后会刷新。

I plunkered a bit on your problem and I did not need to use a $timeout to update the model with updated data. plunkered你的问题一点,我并不需要使用$timeout更新与更新的数据模型。

.controller('AController', function(ptth$) {

    var self = this;

    self.time = 'wait for it';

    self.init = function() {
      ptth$.get().then(function(data) {
        self.time = data;
      });
    };

    self.init();

    self.save = function() {
        ptth$.post().then(function() {
          //I use an alert instead of a div we show or hide.
          window.alert('hey, this is real success');
          //You either re-fetch data here and update your model
          //Or you reload after the alert...

          //This example works with a ng-model update, instead of a reload
          self.init();
        }, function() {
          window.alert('this is failure');
        });
    };
})

Eventhough this is not the way I would set up things, this example should illustrate that you do not really need a $timeout or $reload for that matter. 尽管这不是我设置的方式,但此示例应说明,您实际上不需要$timeout$reload

Inject $timeout and do this changes to your JS Code. 注入$ timeout并将其更改为您的JS代码。

$scope.Save = function (data) {
    debugger;
    $http.post($rootScope.WebApiURL + '/updatemanifeststatus');
    var value= $http.get($rootScope.WebApiURL + '/getmanifeststatus' );

        $scope.manifeststatus = data;

        $scope.showstatus = true;
        $scope.alert = { type: 'success', msg: 'Published Successfully.' };
        $(".statusDivPublish").show();

        //Adding a 5 second delay
        $timeout($scope.Refresh1, 5000);

    }

    $scope.Refresh1 = function () {
        //refresh
        $state.go($state.current, {}, { reload: true });
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM