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

时间:2016-09-11 00:03:51

标签: javascript angularjs

有关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>

它以未定义的形式返回。关于为什么或如何在视图中显示它的想法?感谢

1 个答案:

答案 0 :(得分:0)

想出来。在我的指令中,我必须添加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;
...