2014-11-08 2 views
1

다음 코드를 사용하여 상위 범위 나 $ 범위를 바인딩 할 수 없습니다.

index.html을

<div ng-controller="MainCtrl"> 
... 
    <div ng-include="sidebarbar.url"></div> 
</div> 

main.js 컨트롤러 안에 내 index.html을에서

angular.module('myApp') 
.controller('MainCtrl', ['$scope', 'DataProvider', 'SidebarService', function ($scope, DataProvider, SidebarService) { 
//Initialize 
$scope.data= DataProvider; 
... 

내 데이터 범위에 대한 전체 액세스 권한은 지금은 내부에이 데이터의 일부를 표시해야 사이드 바보기, SiderbarServices 사이드 바를 열고 모든 작품의 선택된 ID를 설정합니다

details.html (ng-include로 열어 본 것입니다)

,
<div ng-controller="DetailsCtrl"> 
    <script type="text/javascript"> 
    console.log('test'); //is displayed 
    console.log(data); //not defined 
    console.log($parent); //not defined 
    console.log(testScope); //not defined 
    </script> 
</div> 

details.js (컨트롤러)

angular.module('myApp').controller('DetailsCtrl', ['$scope', function ($scope) { 
    $scope.data= $scope.$parent.data; 
    $scope.testScope = 'testing if available'; 

    console.log($scope); //is displayed 
    console.log($scope.data); //is displayed 
}]); 

어떻게 내부에서 데이터에 액세스 할 수 있습니다 겨-포함되어 있습니까?

답변

1

사용 details.html이 코드 (나는 이미 라우팅 해요로보기를 사용할 수 없습니다) :

<div ng-controller="DetailsCtrl"> 
    <!-- <script type="text/javascript"> 
    console.log('test'); //is displayed 
    console.log(data); //not defined 
    console.log($parent); //not defined 
    console.log(testScope); //not defined 
    </script> --> 
    <div>{{data}}</div> 
</div> 

및 주요 html로이 코드 교체 :

<div ng-include="'details.html'"></div> 

plunker 참조 작동 중입니다

이 실수를해야합니다 : ng-include="'details.html'" 아니요 ng-include="details.html"

<script> 태그는 ng-controller에서 작동하지 않습니다.

+0

코드에서 시도했지만 여전히 작동하지 않습니다. ng-include가 개체에서 변경되었습니다. 이 부분은 페이지가 열리고 정적 컨텐츠가 표시됩니다. '$ parent.data.key'를 사용하여 작동하도록했습니다. 도움 주셔서 감사합니다 – DouglasDC3

관련 문제