'this' vs $scope in AngularJS controllers

后端 未结 7 1524
逝去的感伤
逝去的感伤 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-21 05:31

    $scope has a different 'this' then the controller 'this'.Thus if you put a console.log(this) inside controller it gives you a object(controller) and this.addPane() adds addPane Method to the controller Object. But the $scope has different scope and all method in its scope need to be accesed by $scope.methodName(). this.methodName() inside controller means to add methos inside controller object.$scope.functionName() is in HTML and inside

    $scope.functionName(){
        this.name="Name";
        //or
        $scope.myname="myname"//are same}
    

    Paste this code in your editor and open console to see...

     
    
    
        
        
        
        this $sope vs controller
    
        
    
    
    
    Comming From $SCOPE :{{firstName}}

    Comming from $SCOPE:{{lastName}}

    Should Come From Controller:{{nickName}}

    Blank nickName is because nickName is attached to 'this' of controller.





    {{message}}

提交回复
热议问题