成功注入后在指令视图中显示常量

Displaying constants within directive view after successfully injecting

本文关键字:显示 常量 视图 指令 注入 成功      更新时间:2023-09-26

关于angularjs中constants的问题。我在app.js中创建了以下常量:

 ...     angular
        .module('blocTime', ['firebase', 'ui.router'])
        .config(config)
        .constant('STOP_WATCH', {
          "workTime": 1500,
          "breakTime": 300
        });
})();

我在指令中注入了常量,如下所示:

(function() {
    function clockTimer($interval, $window, STOP_WATCH) {
        return {
            templateUrl: '/templates/directives/clock_timer.html',
            replace: true,
            restrict: 'E',
            scope: {},
            link: function(scope, element, attributes) {
                console.log(STOP_WATCH.workTime); ...
...   
 angular
        .module('blocTime')
        .directive('clockTimer', clockTimer);

我可以从我的指令中控制台记录常量。然而,我的观点是不渲染常数。HTML:

<div>
  <div class="stop-watch">{{ STOP_WATCH.workTime }}</div>

返回undefined。关于为什么或如何使其在视图中显示的想法?由于

明白了。在我的指令中,我必须添加scope.STOP_WATCH = STOP_WATCH:

(function() {
    function clockTimer($interval, $window, STOP_WATCH) {
        return {
            templateUrl: '/templates/directives/clock_timer.html',
            replace: true,
            restrict: 'E',
            scope: {},
            link: function(scope, element, attributes) {
scope.STOP_WATCH = STOP_WATCH;
...