0

What's the difference between using $scope vs $ctrl? Is it that I'm able to reference $scope data from other components? I'm still a beginner at AngularJS and am trying to understand scope.

angular.
  module('phonecatApp').
  component('phoneList', { 
    template: '<li ng-repeat="phone in $ctrl.phones">'
    + '<span>{{phone.name}}</span>'
    + '<p>{{phone.snippet}}</p>'
    + '</li>',
    controller: function PhoneListController() {
      this.phones = [
        {
          name: 'Nexus S',
          snippet: 'Fast just got faster with Nexus S.'
        }, {
          name: 'Motorola XOOM™ with Wi-Fi',
          snippet: 'The Next, Next Generation tablet.'
        }, {
          name: 'MOTOROLA XOOM™',
          snippet: 'The Next, Next Generation tablet.'
        }
      ];
    }
  });

versus

angular.
  module('phonecatApp').
  component('phoneList', {  
    template: '<li ng-repeat="phone in phones">'
    + '<span>{{phone.name}}</span>'
    + '<p>{{phone.snippet}}</p>'
    + '</li>',
    controller: function PhoneListController($scope) {
      $scope.phones = [
        {
          name: 'Nexus S',
          snippet: 'Fast just got faster with Nexus S.'
        }, {
          name: 'Motorola XOOM™ with Wi-Fi',
          snippet: 'The Next, Next Generation tablet.'
        }, {
          name: 'MOTOROLA XOOM™',
          snippet: 'The Next, Next Generation tablet.'
        }
      ];
    }
  });

  • $scope should be avoided because it is not available in Angular 2+ so it will make the migration more difficult. – georgeawg Apr 14 '19 at 02:50

1 Answers1

0

I can't mark this question as a duplicate, but here you have a link to a question with a great answer. Good luck!

'this' vs $scope in AngularJS controllers

georgeawg
  • 48,608
  • 13
  • 72
  • 95
MSclavi
  • 427
  • 2
  • 10