2016-09-11 3 views
1

Frage bezüglich constants in angularjs. Ich habe die folgenden Konstanten in app.js erstellt:Anzeigen von Konstanten in der Direktivenansicht nach erfolgreicher Injektion

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

ich die Konstante in meinem Richtlinie injiziert habe, wie folgt:

(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); 

Ich kann die Konstante Konsolprotokoll aus meiner Richtlinie nur in Ordnung. In meiner Ansicht wird die Konstante jedoch nicht dargestellt. HTML:

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

Es kommt als undefined zurück. Gedanken darüber, warum oder wie es in der Ansicht angezeigt wird? Danke

+0

Sie verweisen könnte [diese Antwort] (http://stackoverflow.com/a/39430848/2435473) Ihr Problem –

+0

Danke für die Hilfe zu beheben. Es beantwortete meine Frage nicht, aber half mir, auf verschiedene Arten zu fragen, was ich brauchte. Gefunden, was ich hier gebraucht habe: http://stackoverflow.com/questions/24338261/how-to-access-constants-in-angulajs-template. – DustinRW

Antwort

0

Ich habe es herausgefunden. In meinem Richtlinie hatte ich scope.STOP_WATCH = STOP_WATCH hinzuzufügen:

(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; 
... 
Verwandte Themen