简体   繁体   中英

Child scope accessible in parent without 'ng-controller' directive set

I don't understand how this is possible. Please clear this up for me. I am reading about the scope inheritance and, as far as I understood, we cannot access child's scope from the parent, right?

So I have this situation where I created a controller but forgot to set it in the template via ng-controller detective. But it still works. I never noticed it. So is this the expected behaviour or I am just missing something? I din't even know what parts of code to list here.

It is an ordinary controller

angular.module('Basal.tables')

.controller('ListTablesCtrl', function($scope, getTablesS) {
  $scope.tables = {};

  getTablesS.fetch(function (d){
    $scope.tables = d;
    });

});

... executed at location change

when('/tables', {
  templateUrl: '/views/tables/list-tables.html',
  controller: 'ListTablesCtrl'
}).

But there is no mention of the controller in the template.

<div class='frame' id='list-tables'>

<section class='title'> Tables </section>
<table>
  <tr ng-repeat='table in tables'>
    <td><input type='checkbox'></td>
    <td>{{ table.title }}</td>
    <td>{{ table.desc }}</td>
  </tr>
</table>

</div>

Everything works though. How is it possible to see the tables object here? There is the top Main Controller set on the html body which means that it operates under this main controller in the template. But then how does it accesses child's scope? Please explain if I am missing something silly.

Thanks.

Angular looks upwards for a controllers/method. Meaning if it is not in the current scope, it will look into the parent scope.

But in your case, you have have attached the controller in your route file.

when('/tables', { templateUrl: '/views/tables/list-tables.html', controller: 'ListTablesCtrl' })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM