2014-09-29 2 views
1

각도 지시어에서 나중에 사용할 수 있도록 일부 스타일 (예 : backgroundImage)을 저장하고 싶습니다. 대부분의 시간 지시어에서 요소 스타일 가져 오기 - 무작위로 실패 함

angular.module('myApp') 
    .directive('myDirective', function() { 
     return { 
      restrict: 'A', 
      terminal: true, 
      link: function(scope, element, attrs) {         
       // examples of how get it 
       console.log(element.css('background-image')); 
       console.log(window.getComputedStyle(element[0]).backgroundImage); 
      } 
     }; 
    }); 

내가 성공적으로 backgroundImage의 가치를 얻을 수 있지만, timetimes 난 그냥 값을 얻을 : 나는 다음과 코드가 있습니다. 그런 다음 페이지를 새로 고치고 값을 다시 가져옵니다. 이 문제없이 지시문에서 스타일 속성을 가져 오려면 어떻게해야합니까?

모든 의견이나 제안을 부탁드립니다. 감사합니다

답변

0

당신은 확실히 CSS를 한 번 모든 내용이 $viewContentLoaded 이벤트를 대기에 의해로드 된 읽고있는 만들 수 있습니다

angular.module('myApp') 
    .directive('myDirective', function() { 
     return { 
      restrict: 'A', 
      terminal: true, 
      link: function(scope, element, attrs) {         
       scope.$on('$viewContentLoaded', function() { 
        console.log(element.css('background-image')); 
        console.log(window.getComputedStyle(element[0]).backgroundImage); 
       }); 
      } 
     }; 
    }); 
+0

감사하지만, 이벤트 $ viewContentLoaded 결코 트리거되지됩니다. – leticia