2014-12-07 2 views
0

지시어 내에서 ng-controller를 래핑하고 html 내의 해당 속성에 액세스해야합니다. 그러나, 나는 이것을 작동시킬 수 없다.지시문 내에 정의 된 각도 컨트롤러에 액세스

이 내 app.js 파일 내용이다 : 나는 MyController에 내에서 정의 (메시지를 말한다) 모든 속성에 액세스 할 수 없습니다입니다

angular.module('myModule', []) 
.directive('myDirective', function() 
{ 
    transclude: true, 
    template: '<div ng-controller="MyController as ctrl" ng-transclude></div>' 
}); 

. HTML 페이지의이 코드 조각이 작동하지 않습니다 (예 : 메시지가 인쇄되지 않음).

<my-directive> 
    {{ ctrl.message }} 
</my-directive> 

내가 여기에 뭔가를 놓친가요?

+0

당신은 괜찮을 당신이 지시어를 범위 지정하고 있지 않기 때문입니다. 당신이 그 부분을 필요로하지 않는 것처럼 보입니다 –

+0

내 html이 ctrl.message를 표시해야하기 때문에 필요합니다 –

+0

감사합니다 @ EdgarMartinez, 이것이 올바른 링크인지 확실하지 않습니다. –

답변

0
angular.module('myModule', []) 
.controller('MyController', function($scope) { 
    $scope.message = 'Hello from Scope'; 
    $scope.me = this; 
    this.scope = $scope; 
    this.message = 'Hello from Controller'; 
    $scope.click = function(scope) { 
    alert('Alert message!!!'); 
    } 
    this.click = function(){ 
    alert('Controllers click!!!') 
    } 
}) 
.directive('myDirective', function() { 
    return { 
    restrict:'E', 
    scope: { 
     ctrl: "=" 
    }, 
    transclude: true, 
    template: '<div ng-click="ctrl.scope.click()">' 
     +'Scope message:{{ctrl.scope.message}}<br/>Controller message: {{ctrl.message}}<br />' 
     + '</div><div ng-click="ctrl.click()">Click me!</div>'; 
    } 
}); 

당신이이 방법을 시도 할 수 있습니다 ... 당신을 도움이되기를 바랍니다 ... Plunker : 당신은 NG 컨트롤러 = "MyController에 Ctrl 키와 같은"부분을 생략하면 see all code

관련 문제