如何激活两个功能,一个是页面重新加载/刷新另一个ng-click?

时间:2016-08-17 05:38:33

标签: javascript angularjs

这是控制器。

$scope.userhold_payment = function() {
    $http.get('http://localhost/ngaffiliate/api/payment/change_hold_payment_status')
        .then(function(response) {
            console.log('I called');
            $scope.userhold = response.data;
            // console.log($scope.userhold.is_hold_payment);
        });
};
$scope.check_status = function() {
    $http.get('http://localhost/ngaffiliate/api/payment/check_payment_status')
        .then(function(response) {
            console.log('I called now')
            $scope.checkstatus = response.data;
            console.log(response);
            console.log($scope.checkstatus.is_hold_payment);
        });
}

这是main.html

<div ng-init="check_status()">
    <button type="button" ng-click="userhold_payment()" class="btn-success btn-lg hold_payment_html" style="margin-top:7px; font-size:16px">Hold Payment</button>
</div>

重新加载后,我希望函数checkstatus()得到初始化并点击ng-click,userhold_payment()。我该怎么做呢?我在使用ng-init之前使用了$ scope,但每次重新加载时都会触发userhold_payment(),这是我不想要的。

非常感谢!

3 个答案:

答案 0 :(得分:1)

尝试这种方式:

$scope.userhold_payment = function() {
    $http.get('http://localhost/ngaffiliate/api/payment/change_hold_payment_status')
        .then(function(response) {
            console.log('I called');
            $scope.userhold = response.data;
            // console.log($scope.userhold.is_hold_payment);
        });
};

//this will call when ever your controller reinstance
($scope.check_status = function() {
    $http.get('http://localhost/ngaffiliate/api/payment/check_payment_status')
        .then(function(response) {
            console.log('I called now')
            $scope.checkstatus = response.data;
            console.log(response);
            console.log($scope.checkstatus.is_hold_payment);
        });
})();

HTML:

<div>
    <button type="button" ng-click="userhold_payment()" class="btn-success btn-lg hold_payment_html" style="margin-top:7px; font-size:16px">Hold Payment</button>
</div>

最好使用控制器而不是ng-init

答案 1 :(得分:0)

您可以使用window onload事件执行此操作,请查看代码段。

&#13;
&#13;
select t.*
from
    <T> t
    inner join
    (
        select person_id, element_value, max(element_effective_date) as max_date
        from <T>
        where element_effective_date <= @effective_date
        group by person_id
    ) as m
        on     m.person_id = t.person_id
           and m.element_value = t.element_value
           and m.max_date = t.element_effective_date
where
    t.element_value = @element_value
&#13;
var app = angular.module("myapp",[]);

app.controller("demoCtrl",function($scope,$window){

  $window.onload = function(){
   alert("load event");
  }
  $scope.clickFunction =function(){
    alert("ng-click event");
  }
})
&#13;
&#13;
&#13;

答案 2 :(得分:0)

初始化数据的函数调用可能应该在控制器中进行。如果您引入一个名为ngStorage的模块,您可以轻松地在页面刷新期间保持数据,而且工作量极少。

$scope.check_status=function(){
   $http.get('http://localhost/ngaffiliate/api/payment/check_payment_status')
      .then(function(response){
          console.log('I called now')
          $localStorage.checkstatus = $scope.checkstatus = response.data;
          console.log(response);
          console.log($scope.checkstatus.is_hold_payment);
        });
}
if ($localStorage.checkstatus){
    $scope.checkstatus = $localStorage.checkstatus;
}
else{
    $scope.check_status();
}