如何将此间隔服务连接到视图?

IT技术 javascript angularjs
2021-02-26 15:15:51

AngularJS 文档$interval给出了$interval在控制器中使用来管理用户可以在视图中使用的计时器的示例您可以通过单击此链接在 angularJS 文档页面阅读官方示例的代码

我试图将代码从示例控制器移回服务,以便代码可以更加module化。但是应用程序没有将服务连接到视图。我在plnkr 中重新创建了该问题,您可以通过单击此链接来使用它

需要对上面 plnkr 中的代码进行哪些具体更改,以便该mytimer服务可以作为导入服务的控制器的属性提供给视图?

在这里总结一下,'index.html` 是:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-example109-production</title>
    <script src="myTimer.js" type="text/javascript"></script>
    <script src="exampleController.js" type="text/javascript"></script>
    <script src="app.js" type="text/javascript"></script>

    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>

</head>
<body ng-app="intervalExample">

<div>
  <div ng-controller="ExampleController">
    <label>Date format: <input ng-model="mytimer.format"></label> <hr/>
    Current time is: <span my-current-time="mytimer.format"></span>
    <hr/>
    Blood 1 : <font color='red'>{{mytimer.blood_1}}</font>
    Blood 2 : <font color='red'>{{mytimer.blood_2}}</font>
    <button type="button" data-ng-click="mytimer.fight()">Fight</button>
    <button type="button" data-ng-click="mytimer.stopFight()">StopFight</button>
    <button type="button" data-ng-click="mytimer.resetFight()">resetFight</button>
  </div>
</div>
</body>
</html>

的代码app.js是:

angular.module('intervalExample',['ExampleController'])

的代码exampleController.js是:

angular
.module('intervalExample', ['mytimer'])
.controller('ExampleController', function($scope, mytimer) {

    $scope.mytimer = mytimer;

});

的代码myTimer.js是:

angular
.module('mytimer', [])
.service('mytimer', ['$rootScope', function($rootScope, $interval) {

    var $this = this;
    this.testvariable = "some value. ";

        this.format = 'M/d/yy h:mm:ss a';
        this.blood_1 = 100;
        this.blood_2 = 120;

        var stop;
        this.fight = function() {
          // Don't start a new fight if we are already fighting
          if ( angular.isDefined(stop) ) return;

          stop = $interval(function() {
            if (this.blood_1 > 0 && this.blood_2 > 0) {
              this.blood_1 = this.blood_1 - 3;
              this.blood_2 = this.blood_2 - 4;
            } else {
              this.stopFight();
            }
          }, 100);
        };

        this.stopFight = function() {
          if (angular.isDefined(stop)) {
            $interval.cancel(stop);
            stop = undefined;
          }
        };

        this.resetFight = function() {
          this.blood_1 = 100;
          this.blood_2 = 120;
        };

        this.$on('$destroy', function() {
          // Make sure that the interval is destroyed too
          this.stopFight();
        });

}])

    // Register the 'myCurrentTime' directive factory method.
    // We inject $interval and dateFilter service since the factory method is DI.
    .directive('myCurrentTime', ['$interval', 'dateFilter',
      function($interval, dateFilter) {
        // return the directive link function. (compile function not needed)
        return function(scope, element, attrs) {
          var format,  // date format
              stopTime; // so that we can cancel the time updates

          // used to update the UI
          function updateTime() {
            element.text(dateFilter(new Date(), format));
          }

          // watch the expression, and update the UI on change.
          scope.$watch(attrs.myCurrentTime, function(value) {
            format = value;
            updateTime();
          });

          stopTime = $interval(updateTime, 1000);

          // listen on DOM destroy (removal) event, and cancel the next UI update
          // to prevent updating time after the DOM element was removed.
          element.on('$destroy', function() {
            $interval.cancel(stopTime);
          });
        }
      }]);;

以上所有代码都在此 plnkr以“工作”形式组装,您可以使用它来诊断和确定问题的解决方案。 那么需要对上面的代码做哪些具体的修改,才能让用户通过视图与服务进行交互呢?

1个回答

首先,您没有将 $interval 注入mytimer服务并尝试使用它。

其次,您在mytimer服务中遇到了范围问题

stop = $interval(function() {
    if (this.blood_1 > 0 && this.blood_2 > 0) {
        this.blood_1 = $this.blood_1 - 3;
        this.blood_2 = $this.blood_2 - 4;
    } else {
        this.stopFight();
    }
}, 100);

在声明一个函数时,您正在创建一个新的作用域,这意味着this它指向一个新的作用域。您可以使用bind或使用$this您在第 5 行中声明变量。(在 ES2015 中,您可以简单地使用箭头函数)。

您还两次在app.js和 中声明了module exampleController mytimer.js

看看这个工作的Plunker:http ://plnkr.co/edit/34rlsjzH5KWaobiungYI?p=preview

@AlonEitan plnkr 只是我实际应用程序的简化版本。我挑剔的原因是我的实际应用程序有其他控制器,因此删除文件或注释此 app.js 中唯一一行的解决方案不适用于在同一应用程序中具有多个控制器的现实世界。
2021-04-21 15:15:51
@AlonEitan 您的参与使我能够找到答案。我会尝试将其添加为对他的答案的编辑,但会在此处发布以防编辑失败。我将编辑添加到他的答案中,以试图给予他荣誉,同时也让其他用户以后更容易阅读。答案是更改exampleController.js为以下内容: angular .module('ExampleController', ['mytimer']) .controller('ExampleController', ['$scope', 'mytimer', function($scope, mytimer) { $scope.mytimer = mytimer; }]);
2021-04-27 15:15:51
@CodeMed Dan 和我都来自以色列,现在是凌晨 1 点 26 分,所以我明天再看看
2021-05-07 15:15:51
@AlonEitan 谢谢!不过,您是否愿意帮助我真正了解根本问题?我的问题是如何设置它以便多个控制器可以成为同一个应用程序的一部分,来自 index.html 的 ng-app 指向 app.js,它反过来管理应用程序中的所有各种控制器,包括但不仅限于 ExampleController。我正在努力了解这一切是如何运作的。
2021-05-13 15:15:51
@CodeMed - 按照要求,只需angular.module('intervalExample',['ExampleController']);app.js注释掉该行,它就可以正常工作
2021-05-19 15:15:51