2013-09-25 2 views
0

사용자가 권한이 있는지 여부에 따라 Tooltip (from AngularJs Bootstrap UI)을 표시하는 지시문을 만들려고합니다.AngularJs Tooltip 지시문

이 일을 잘 수행하고이 tooltiptooltip-position 하지만 툴팁이를 표시하지 않는 속성을 필수 추가 할 수 있습니다.

내 코드에서 생성 된 요소와 툴팁이 일반 html 인 요소를 비교하면 class="ng-scope"을 제외하고는이 클래스를 수동으로 추가해도 도움이되지 않습니다.

proGamersApp.directive('registered', ['$rootScope', 'authService', function ($rootScope, authService) { 
    return { 
     restrict: 'A', 
     scope: true, 
     link: function ($scope, element, attrs) { 
      element.addClass('faded'); 

      $rootScope.$watch('user.role', function (role) { 
       $scope.$apply(function() { 
        var accessLevel = routingConfig.accessLevels[attrs.accessLevel]; 
        if (!authService.authorize(accessLevel)) { 
         element.attr('tooltip-placement', 'bottom'); 
         element.attr('tooltip', 'Avaiable for registered users.'); 
        } else 
         element.attr('tooltip-placement', 'bottom'); 
        element.attr('tooltip', 'Avaiable for registered users.'); 
       }); 
      }); 
     } 
    }; 
}]); 

어떤 생각의 사람 : 여기

내 지시 코드? 3

업데이트는 그것의 정의되지 않은 함수를 말한다 이후, 을 '$compile(element)을 제거하고 $ 적용 기능의 사용을 변경했습니다. 여전히 '$ digest already in progress'오류가 발생합니다.

새로운 코드 :

proGamersApp.directive('registered', ['$rootScope', 'authService', function ($rootScope, authService, $compile) { 
    return { 
     restrict: 'A', 
     scope: true, 
     link: function ($scope, element, attrs) { 
      element.addClass('faded'); 

      $rootScope.$watch('user.role', function (role) { 
       var accessLevel = routingConfig.accessLevels[attrs.accessLevel]; 
       if (!authService.authorize(accessLevel)) { 
        element.attr('tooltip-placement', 'bottom'); 
        element.attr('tooltip', 'Avaiable for registered users.'); 
       } else { 
        element.attr('tooltip', 'Avaiable for registered users.'); 
       } 

       $scope.$apply(element); 
      }); 

     } 
    }; 
}]); 
+0

은 $ 컴파일()'당신의 요소'에보십시오. – AlwaysALearner

+0

내가 제대로했는지는 확실하지 않지만 추가 (업데이트 참조)하고 변경된 사항은 없습니다. –

+0

업데이트 된 게시물을 살펴 보았으므로 추가했습니다. –

답변

1

이 같은 $compile를 사용해보십시오 :

proGamersApp.directive('registered', ['$rootScope', 'authService', '$compile', 
function ($rootScope, authService, $compile) { 
    return { 
     restrict: 'A', 
     scope: true, 
     link: function ($scope, element, attrs) { 
      element.addClass('faded'); 
      $rootScope.$watch('user.role', function (role) { 
       var accessLevel = routingConfig.accessLevels[attrs.accessLevel]; 
       if (!authService.authorize(accessLevel)) { 
        element.attr('tooltip-placement', 'bottom'); 
        element.attr('tooltip', 'Avaiable for registered users.'); 
       } else { 
        element.attr('tooltip', 'Avaiable for registered users.'); 
       } 
       $compile(element.parent().contents())($scope); 
      }); 
     } 
    }; 
}]); 
+0

제대로 작동하려면 코드 수정이 필요했지만 문제가 해결되었습니다. 왜 'element'대신 'element.parent(). contents()'를 사용했는지 설명해 주시면 감사하겠습니다. –

+0

'contents()'가 자식 요소를 반환하는 동안 우리는 요소 자체를 컴파일하기를 원하기 때문입니다. 너무 확실하지는 않지만 : D – AlwaysALearner

1

는 범위 액세스를 결정하는 변수 또는 함수의 단지 사용할 수 있도록 컨트롤러를 가지고하는 것입니다 이런 종류의 일을 처리 할 수있는 또 다른 방법은 다음 NG 숨기기를 사용 그리고 툴팁을 설치하기 위해 dom에있는 ng-show 등등.

<div data-ng-controller="SessionCtrl"> 
    <div data-ng-hide="session.active()"> 
     put your logged in user stuff here 
    </div> 
    <div data-ng-hide="session.active()"> 
     put your non logged in user stuff here 
    </div> 
</div> 
+0

고마워,하지만 나는 'authService'를 사용하여 'accessLevel'지시어를 사용하는 더 깨끗한 이래로 내가 한 방식을 선호한다. 내 문제는 툴팁이 보이도록 만드는 것입니다. 부트 스트랩 툴팁이 내 코드와 작동하는 방식에 문제가있는 것 같습니다 (요소 자체는 같지만 툴팁이 표시되지 않습니다). –

+0

좋아, 그럼 다른 사람이 무슨 말을했는지 살펴 보라. $ 컴파일 서비스를 지시문에 삽입 할 수 있고 컴파일 기능을 만들 수는 없다. 그런 다음 부트 스트랩 지시문 속성을 추가 한 후에 요소를 컴파일합니다. – aet

+0

도움을 주셔서 감사합니다. 나는 당신이 말한 곳에서 그것을 추가했습니다, 나는 'Error : $ digest already in progress'를 얻었고, 업데이트 된 포스트를 보았습니다. –