2014-03-18 3 views
0

내가 작업을 다음과 같은 두 가지 지침을 말해봐 서로 내가 parent의 컨트롤러 내에서 ngModelController 사용 방법AngularJS와 지시어 사용에 필요한 컨트롤러는

directive('parent', function() { 
    return { 
     scope: {}, 
     require: 'ngModel', 
     controller: function($scope) { 
      this.doSomething = function() { 
       //How to use ngModelController here? 
      }; 
     }, 
     link: function(scope, element, attr, ngModelController) { 
     } 
    }; 
}); 

directive('child', function() { 
    return { 
     scope: {}, 
     require: '^parent', 
     link: function(scope, element, attr, parent) { 
      parent.doSomething(); 
     } 
    }; 
}); 

? 나는 scope.ngModelController = ngModelController을 링크 함수 안에 넣을 수 있다고 생각하지만 그것은 해커처럼 보입니다. 더 나은 해결책이 있습니까?

+0

링크 기능의 범위에 넣을 수 있을까요? 약간 해키처럼 보이지만 다른 것을 생각할 수는 없습니다 ... – aet

+0

요구 사항 : '? ngModel'을 (를) 사용해보십시오. 그것은 컨트롤러를 찾는 부모를 통해 거품이 날 것입니다. –

+0

분명히 이것은 중복되었습니다! http://stackoverflow.com/questions/21231294/angularjs-inject-required-directive-controller-into-the-controller-instead-of-t – grivescorbett

답변

0

더 적은 해커입니까?

directive('parent', function() { 
    var ngModelCtrl = null; 
    return { 
     scope: {}, 
     require: 'ngModel', 
     controller: function($scope) { 
      this.doSomething = function() { 
       //How to use ngModelController here? 
       if (ngModelCtrl) { 
       } 
      }; 
     }, 
     link: function(scope, element, attr, ngModelController) { 
      ngModelCtrl = ngModelController; 
     } 
    }; 
}); 
관련 문제