2015-01-06 1 views
0

ng 클래스을 기반으로하는 클래스가있는 글로벌 키보드 바로 가기를 누르면 특정 지시문 인스턴스 에서 함수를 호출 할 수 있는지 궁금합니다. 여기 키보드 명령에 대한 특정 지시문 인스턴스에 대한 함수 호출

몇 가지 템플릿 코드 :

home.html을

<body ng-controller="SampleController"> 
    <test-question 
     ng-repeat="n in [1,2,3]" 
     ng-class="{'focused': tracer.focus == n}" 
     focusnum = "{{n}}" 
     ng-click="tracer.focus = n" 
     > 
     Test Question {{n}} 
    </test-question> 
</body> 

home.js

angular.module('sampleApp', []) 
    .controller("SampleController", function($scope) { 
    $scope.tracer = { 
     focus: 1 
    } 
    // on keyboard shortcut call some function on a specific directive with class focused 
    }) 
    .directive('testQuestion', function() { 
    return { 
     restrict: 'E', 
     link: function(scope, el, attrs) { 
      scope.someFunction = function() {}; 
     } 
    } 
    }); 

http://plnkr.co/edit/P7rM3o4TADSf6rxTIRsZ?p=info

답변

0

에서 오는 시도 다른 각도에서. 주 컨트롤러의 값에 바인딩되는 지시문을 만듭니다. 예를 들어, 지시어가 값을 보았을 때 true로 설정되면 지시문 내에서 메소드를 호출합니다.

메인 컨트롤러에서 키 누르기 바로 가기를 설정하면 메인 컨트롤러에서도 유지 관리되는 포커스를 기반으로 필요한 값을 true로 설정할 수 있습니다. 이 같은 것이

 angular.module('sampleApp', []).controller("SampleController", function($scope) { 

     $scope.childDirectiveFlags = {dir1:false,dir2:false,dir3:false}; 
     $scope.currentFocus = 'dir1'; 

     // on keypress event set the flag to true in the directive you want action to 
     // happen on 

     $scope.childDirectiveFlags[$scope.currentFocus] = true; // Will trigger watch 
    }; 

    sampleApp.directive = directive('testQuestion', function() { 
     return { 
     restrict: 'E', 
     scope : { 
      watchValue: '@' 
     }, 
     link: function(scope, el, attrs) { 

      scope.$watch('watchValue',function() { 
       if(scope.watchValue) { 
        scope.someFunction(); 
        scope.watchValue = false; 
       } 
      }); 
      scope.someFunction = function() {}; 
      } 
     } 
    }); 
+0

감사합니다! 모델에서 $ scope.childDirectiveFlags = {dir1 : false, dir2 : false, dir3 : false};를 저장하지 않고이 작업을 수행 할 수 있습니까? 우리는 이미 지시어를 가지고 있기 때문에 특정한 지시어를 목표로하기 위해 대신 그 속성을 사용하는 방법이있을 것이라고 생각하고 있습니다. –

+0

물론 $ rootscope.broadcast 기능이 있습니다. 그러나 이것은 여전히 ​​가장 효율적인 방법으로 보이지 않는 모든 지시어를 호출합니다. –

+0

물론, 바인딩하는 한 모든 값을 사용할 수 있습니다. 이는 단지 예일뿐입니다. – Scott

관련 문제