2014-11-08 21 views
1

使用下面的代码我无法获得我的父范围或$ scope来绑定。

的index.html

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

main.js

angular.module('myApp') 
.controller('MainCtrl', ['$scope', 'DataProvider', 'SidebarService', function ($scope, DataProvider, SidebarService) { 
//Initialize 
$scope.data= DataProvider; 
... 
在我的控制器内的index.html

我已经完全进入我的数据范围,现在我需要里面显示该数据的部分侧边栏视图,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 
}]); 

如何从内部获得访问数据的NG-包括哪些内容? (因为我已经路由我不能使用视图)

回答

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 2014-11-08 14:00:30

相关问题