2014-11-06 3 views
0

죄송합니다. 이전에이 질문을했으나 문제점이 조금 다르고 누군가 나를 도울 수 있기를 바랍니다.지시문 안에 각도 함수를 호출하는 중

1) 많은 버튼이있는 지시문이 있습니다. 이 버튼과 지시문에서 함수를 호출하고 싶습니다. 하지만 어떻게해야할지 모르겠다.

// Directive for the login header to avoid the duplication of the code 
angular.module('App').directive('mainHeader',function(){ 
    return{ 
     restrict: 'AE', 
     template:'<h1 class="logo"> My App </h1>'+ 
     '<button class="btn btn-primary">New User</button>'+ 
     '<button class="btn btn-primary">New Product</button>'+ 

      '<span class="dropdown" on-toggle="toggled(open)">'+ 
       '<a href class="dropdown-toggle">'+ 
       '<img class="userProfile" src="" alt="User Profile">'+ 
       '<b class="caret"></b>'+ 
       '</a>'+ 
        // Here on my profile ???? 
       '<ul class="dropdown-menu pull-right">'+ 
        '<li> My Profile </a> </li>'+ 
        '<li class="divider"></li>'+ 
         // Here on my logout ?????? 
         // This does not work 
        '<li> <a href="" ng-click="logOut()"> Sign Out </li>'+ 
       '</ul>'+ 
      '</span>' 
    } 
}); 

내 컨트롤러

(function() { 
    var logOutController = function($scope){ 
     $scope.logOut = function(){ 
      // Want to call this 
     } 
    logOutController .$inject = ['$scope']; 
    angular.module('App').controller('logOutController ',logOutController); 
}()); 

그리고 내보기 내가이

업데이트 작업을 수행하는 방법을 모르는

<div main-header></div> 

한 줄 것 1 : -

P를보십시오. 멍청한 사람 http://plnkr.co/edit/YONVyVvNm4pQGFMU6SkG?p=preview.

답변

1

당신은 당신의 지시에 분리 범위를 추가해야합니다 다음 일이

<div main-header on-log-out="logOut()"></div> 

Plunker Example

+0

,하지만 약 :

// Directive for the login header to avoid the duplication of the code angular.module('App').directive('mainHeader', function() { return { restrict: 'AE', template: '<h1 class="logo"> My App </h1>' + '<button class="btn btn-primary">New User</button>' + '<button class="btn btn-primary">New Product</button>' + '<span class="dropdown" on-toggle="toggled(open)">' + '<a href class="dropdown-toggle">' + '<img class="userProfile" src="" alt="User Profile">' + '<b class="caret"></b>' + '</a>' + // Here on my profile ???? '<ul class="dropdown-menu pull-right">' + '<li> My Profile </a> </li>' + '<li class="divider"></li>' + // Here on my logout ?????? // This does not work '<li> <a href="" ng-click="logOut()"> Sign Out </li>' + '</ul>' + '</span>', scope: { 'logOut': '&onLogOut' //<- this ties your directive's logOut function to an attr on the HTML tag } } }); 

그런 다음 당신은 당신의 HTML을 수정할 다른 버튼? 어느 것을 클릭 했습니까? 그리고 그것의 동일한 지침서에 – GeekOnGadgets

+0

죄송합니다, 그걸 보지 못했습니다. 'dropdown'은 다른 지시어입니까? 정말로 알려주려면'mainHeader'에 대한 나머지 코드를 보여줘야합니다. 그러나 비슷한 접근법이어야합니다. –

+0

같은 지시어에있는 모든 내용을 무시하십시오. 이 지시문에는 버튼이 있으며이 버튼에 다른 기능을 호출하려고합니다. 코드 중복이 없습니다. – GeekOnGadgets

관련 문제