'this' vs $scope in AngularJS controllers

后端 未结 7 1510
逝去的感伤
逝去的感伤 2020-11-21 05:18

In the \"Create Components\" section of AngularJS\'s homepage, there is this example:

controller: function($scope, $element) {
  var panes = $scope.panes = [         


        
7条回答
  •  攒了一身酷
    2020-11-21 05:42

    The reason 'addPane' is assigned to this is because of the directive.

    The pane directive does require: '^tabs', which puts the tabs controller object from a parent directive, into the link function.

    addPane is assigned to this so that the pane link function can see it. Then in the pane link function, addPane is just a property of the tabs controller, and it's just tabsControllerObject.addPane. So the pane directive's linking function can access the tabs controller object and therefore access the addPane method.

    I hope my explanation is clear enough.. it's kind of hard to explain.

提交回复
热议问题