angularjs - AngularJs中$scope对象的继承

标签 angularjs

我正在学习 AngularJS。我对 $scope 对象的继承感到困惑。就像我希望 $scope 对象特定于 Controller 一样。它如何将值继承给其他 Controller 。以下代码取自官方文档

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-example9-production</title>
  <link href="app.css" rel="stylesheet" type="text/css">
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular.min.js"></script>
  <script src="app.js"></script>    
</head>
<body ng-app="scopeInheritance">
  <div class="spicy">
  <div ng-controller="MainController">
    <p>Good {{timeOfDay}}, {{name}}!</p>

    <div ng-controller="ChildController">
      <p>Good {{timeOfDay}}, {{name}}!</p>

      <div ng-controller="GrandChildController">
        <p>Good {{timeOfDay}}, {{name}}!</p>
      </div>

        <div ng-controller="GrandChildController2">
        <p>Good {{timeOfDay}}, {{name}}!</p>
      </div>
    </div>
  </div>
</div>
</body>
</html>

App.js

(function(angular) {
  'use strict';
var myApp = angular.module('scopeInheritance', []);
myApp.controller('MainController', ['$scope', function($scope) {
  $scope.timeOfDay = 'morning';
  $scope.name = 'Nikki';
}]);
myApp.controller('ChildController', ['$scope', function($scope) {
   $scope.timeOfDay = 'morning2';
  $scope.name = 'Mattie';
}]);
myApp.controller('GrandChildController', ['$scope', function($scope) {
  $scope.timeOfDay = 'evening';
  $scope.name = 'Gingerbread Baby';
}]);
myApp.controller('GrandChildController2', ['$scope', function($scope) {

}]);
})(window.angular);

GrandChildController2 Controller 未更改 $scope 对象的名称和 timeOfDay 属性。我期望输出应该是这样的 timeOfDay ='evening'name = 'Gingerbread Baby'(如果它继承自父级)。但令人惊讶的是,GrandChildController2 Controller 的输出是 timeOfDay ='Goodmorning2'name ='Mattie'。这怎么可能?

请解释一下

谢谢 马尼瓦南

最佳答案

正如您在下面的架构中看到的,GrandChildControllerGrandChildController2 是兄弟,也是 ChildController 的儿子(正如他们的名字所暗示的那样)。

  <div ng-controller="MainController">
  |
  |-- <p>Good {{timeOfDay}}, {{name}}!</p>       <!-- Good morning, Nikki'! -->
  |
  |   <!-- inherits from MainController and then overwrite -->
  |-- <div ng-controller="ChildController">
      |-- <p>Good {{timeOfDay}}, {{name}}!</p>   <!-- Good morning2, Mattie! -->
      |
      |   <!-- inherits from ChildController and then overwrite -->
      |-- <div ng-controller="GrandChildController">
      |     <p>Good {{timeOfDay}}, {{name}}!</p> <!-- Good evening, Gingerbread Baby! -->
      |-- </div>
      |
      |   <!-- inherits from ChildController and does NOT overwrite -->
      |-- <div ng-controller="GrandChildController2">
      |     <p>Good {{timeOfDay}}, {{name}}!</p> <!-- Good morning2, Mattie! -->
      |-- </div>
      |
  |-- </div>  <!-- CLOSE ChildController -->
  </div>  <!-- CLOSE MainController -->

因此,GrandChildController2 从其父 ChildController 继承 $scope,其中 $scope.timeOfDay = 'morning2'$scope.name = 'Mattie'

您的期望

要获得您期望的行为(GrandChild2 继承自 GrandChild),架构必须如下所示:

  <div ng-controller="MainController">
  |
  |-- <p>Good {{timeOfDay}}, {{name}}!</p>       <!-- Good morning, Nikki'! -->
  |
  |   <!-- inherits from MainController and then overwrite -->
  |-- <div ng-controller="ChildController">
      |-- <p>Good {{timeOfDay}}, {{name}}!</p>   <!-- Good morning2, Mattie! -->
      |
      |   <!-- inherits from ChildController and then overwrite -->
      |-- <div ng-controller="GrandChildController">
          |-- <p>Good {{timeOfDay}}, {{name}}!</p> <!-- Good evening, Gingerbread Baby! -->
          |
          |   <!-- inherits from GrandChildController and does NOT overwrite -->
          |-- <div ng-controller="GrandChildController2">
          |     <p>Good {{timeOfDay}}, {{name}}!</p> <!-- Good evening, Gingerbread Baby! -->
          |-- </div>
          |
      |-- </div>  <!-- CLOSE GrandChildController -->
  |-- </div> <!-- CLOSE ChildController -->
  </div> <!-- CLOSE MainController -->

关于angularjs - AngularJs中$scope对象的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34309550/

相关文章:

javascript - 如何使用 Angular.js 中的某些唯一值从数组中删除值

angularjs - 如何获取多个选中的复选框值

html - 如何使用来自 Controller 的参数调用函数进入 View 以及如何将该函数绑定(bind)到angularjs中的指令

asynchronous - Trigger.io + Angular.js 并在调用 forge.ajax 后更新 View

javascript - 在 ng-if AngularJS 中使用 ng-repeat

javascript - AngularJs 无法读取动态设置的属性

javascript - 如何在 Zeppelin 上通过 JavaScript 设置 Angular 变量值

angularjs - scope.$on 在指令中创建时不起作用

javascript - 绑定(bind)为空时的 Angular 默认值

javascript - angular-chart.js legendCallback