$ scope和$ ctrl有什么区别,什么时候应该使用?

时间:2019-04-13 21:39:20

标签: angularjs

使用$ scope和$ 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.'
        }
      ];
    }
  });

1 个答案:

答案 0 :(得分:0)

我不能将此问题标记为重复问题,但是here you have a link是答案很好的问题。祝你好运!

'this' vs $scope in AngularJS controllers