如何绑定到服务方法?

时间:2022-07-17 19:03:51

In this first example, the the view is updated everytime the return value of the method sessionStatus() of userService changes.

在第一个示例中,每次userService方法sessionStatus()的返回值更改时,视图都会更新。

but If a change the sessionStatus method like in the second example :

但如果更改sessionStatus方法,如第二个示例中所示:

sessionStatus: function(){
  return Date.now();
},

it no longer refreshes "live" in the view. I would expect to see the date in milliseconds progressing in the view.

它不再刷新视图中的“实时”。我希望在视图中看到以毫秒为单位的日期。

Is it because the second example, the value returned by the sessionStatus method is changing to fast ? Why isn't it updating the view ?

是因为第二个例子,sessionStatus方法返回的值正在变为快速?为什么不更新视图?

1 个解决方案

#1


1  

The value is not updating because there is no AngularJS event to cause a digest cycle:

该值未更新,因为没有AngularJS事件导致摘要周期:

Add a button that has an ng-click and the value will update everytime it is clicked:

添加一个具有ng-click的按钮,每次单击时该值都会更新:

<body ng-app="myServiceModule">
    <div id="simple" ng-controller="MyController">

  <p>I would expect the number bellow to be ticking</p>
    <p>Session Status: {{sessionStatus()}}</p>
  </div>
  <button ng-click="">Update</button>
</body>

By clicking on the Update button, an AngularJS digest cycle gets initiated and the session status updates.

通过单击“更新”按钮,将启动AngularJS摘要周期并更新会话状态。

The DEMO on PLNKR.

PLNKR上的DEMO。


Or use the $interval service to initiate a repeating digest cycle:

或者使用$ interval服务启动重复的摘要周期:

  angular.
   module('myServiceModule', []).
    controller('MyController', function ($scope, $interval, userService) {
      $scope.sessionStatus = function(){
       return userService.sessionStatus();
      };

      $interval(null, 250);

    })

In this example, the $interval statement starts a digest cycle every 250 milliseconds.

在此示例中,$ interval语句每250毫秒启动一个摘要周期。

The DEMO on PLNKR

PLNKR上的DEMO

#1


1  

The value is not updating because there is no AngularJS event to cause a digest cycle:

该值未更新,因为没有AngularJS事件导致摘要周期:

Add a button that has an ng-click and the value will update everytime it is clicked:

添加一个具有ng-click的按钮,每次单击时该值都会更新:

<body ng-app="myServiceModule">
    <div id="simple" ng-controller="MyController">

  <p>I would expect the number bellow to be ticking</p>
    <p>Session Status: {{sessionStatus()}}</p>
  </div>
  <button ng-click="">Update</button>
</body>

By clicking on the Update button, an AngularJS digest cycle gets initiated and the session status updates.

通过单击“更新”按钮,将启动AngularJS摘要周期并更新会话状态。

The DEMO on PLNKR.

PLNKR上的DEMO。


Or use the $interval service to initiate a repeating digest cycle:

或者使用$ interval服务启动重复的摘要周期:

  angular.
   module('myServiceModule', []).
    controller('MyController', function ($scope, $interval, userService) {
      $scope.sessionStatus = function(){
       return userService.sessionStatus();
      };

      $interval(null, 250);

    })

In this example, the $interval statement starts a digest cycle every 250 milliseconds.

在此示例中,$ interval语句每250毫秒启动一个摘要周期。

The DEMO on PLNKR

PLNKR上的DEMO