$ scope vs $ ctrl之间有什么区别?什么时候应该使用? [重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

使用$ scope vs $ ctrl有什么区别?是否我可以从其他组件引用$ scope数据?我还是AngularJS的初学者,我正在努力了解范围。

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.'
        }
      ];
    }
  });

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.'
        }
      ];
    }
  });

angularjs
1个回答
0
投票

我不能把这个问题标记为重复,但here you have a link对一个问题有一个很好的答案。祝好运!

'this' vs $scope in AngularJS controllers

© www.soinside.com 2019 - 2024. All rights reserved.