2016-07-29 2 views
0

범위 매개 변수와 ng-controler가 모두있는 지시문을 만들려고합니다.scope와 ng-controller가있는 사용자 정의 angularjs 지시문

이이 지시어는 보는 방법이다 :

<csm-dir name="scopeParam" ng-controller="RegisteredController"> 
    <!--Here I want to have content--> 
    {{name}} 
</csm-dir> 

나는 "RegistertedController는"내 응용 프로그램 어딘가에 선언했다.

angular.module('app') 
    .controller('RegisteredController', ['$scope', function ($scope) { 
     //$scope.name should be accessible here 
    }]); 

그리고 지시를 선언하려고 :

angular.module('app') 
    .directive('csmDir', [function() { 
     return { 
      restrict: 'AE', 
      replace: true, 
      transclude: true, 
      scope: { 
       name: '=' 
      }, 
      template: '<section class="layout"><ng-transclude></ng-transclude></section>', 
      link: function (scope, element, attr, ctrl, transclude) { 
       element.find('ng-transclude').replaceWith(transclude()); 
       //here I also need scope.name parameter 
      } 
     }; 
    }); 

내가 얻을 오류 :

Multiple directives [ngController, csmDir] asking for new/isolated scope on: <csm-dir name="scopeParam" ng-controller="RegisteredController"> 

지금이 오류는 매우 유익, 나는대로 작동하지 않습니다는 각 취득 나는 여기에서 싶다.

지시어에서 "scope : {..}"매개 변수를 제거하고 컨트롤러에서 정의 할 수 있지만 필요한 것은 아닙니다.

기본적으로 ng-controller를 사용자 지정 지정 문에 제공 한 다음 해당 컨트롤러에 대한 추가 범위 변수를 제공해야합니다.

어떻게하면됩니까?

+0

은 AngularJS 구성 요소로 충분하지 않습니까? – doge1ord

답변

0

지시어를 태그로 감싸고 거기에 컨트롤러를 놓아야합니다.

<div ng-controller="RegisteredController"> 
    <csm-dir name="scopeParam"> 
     <!--Here I want to have content--> 
     {{name}} 
    </csm-dir> 
</div> 

하나의 태그에서 두 개의 격리 된 범위를 얻는 방법은 없습니다.

+0

그래, 그렇게 할 수는 있겠지만 다른 지시문 안에 컨트롤러 컨트롤러 지시문을 넣을 수 있는지 궁금한데 –

+0

재미있는 일을 할 수 있습니다. 상위 지시문을 작성하고 제어기와 링크하십시오. 그런 다음 require 옵션과 함께 child 지시문을 추가하십시오. 그리고 컨트롤러에 액세스하십시오. 괜찮 니? – Vitalii

관련 문제