根范围与范围 angularJs

rootscope vs scope angularJs

我是 angularJs 的新手,我需要你的帮助。

我有以下示例,我试图从内页对父页上的元素执行操作,但我不明白为什么它的行为很奇怪:我无法设置默认值或使用作用域的操作,但我可以使用 rootScope 来做到这一点。同时我无法使用 rootScope 读取它的值,但我可以使用范围读取它。

这是我的例子:

这是父页面内容包装器:

<div class="page-content-wrapper">
    <div ng-class="settings.layout.pageContent">
        <!-- BEGIN STYLE CUSTOMIZER(optional) -->
        <!-- END STYLE CUSTOMIZER -->
        <!-- BEGIN ACTUAL CONTENT -->
        <div class="page-bar">
            <div ncy-breadcrumb></div>
            <div class="page-toolbar" ng-show="settings.layout.showHeaderTools">
                <div id="date-range" class="pull-right tooltips btn btn-sm" data-container="body" data-placement="bottom" data-original-title="Change dashboard date range">
                    <i class="icon-calendar"></i>&nbsp;
                    <span class="thin uppercase hidden-xs"></span>&nbsp;
                    <i class="fa fa-angle-down"></i>
                </div>
                <div class="actions">
                    <select class="form-control" ng-change="changeSchema();" ng-model="selectedSchema">
                        <option value="A">Schema A</option>
                        <option value="B">Schema B</option>
                        <option value="C">Schema C</option>
                        <option value="D">Schema D</option>
                        <option value="E">Schema E</option>
                    </select>
                </div>
            </div>
        </div>
        <h3 class="page-title hidden-print" data-ng-bind="$state.current.data.pageTitle"></h3>
        <div ui-view class="fade-in-up"> </div>
        <!-- END ACTUAL CONTENT -->
    </div>
</div>

这是内页的控制器:

angular.module('MetronicApp').controller('dashboardController', function ($rootScope, $scope, $http, $timeout, NgMap) {
    $scope.$on('$viewContentLoaded', function () {
        // initialize core components
        App.initAjax();
    });

    $scope.data = {};
    // set sidebar closed and body solid layout mode
    $rootScope.settings.layout.pageContentWhite = true;
    $rootScope.settings.layout.pageBodySolid = false;
    $rootScope.settings.layout.pageSidebarClosed = true;
    $rootScope.settings.layout.showHeaderTools = true;
    $rootScope.settings.layout.pageContent = 'page-content bg-grey-steel';
    $scope.isLoading = false;
    $scope.data.selectedKPI = "Profit";


    $rootScope.selectedSchema = "A";
    $rootScope.changeSchema = function () {
        console.log($scope.selectedSchema);
    };
});

在我的示例中,$rootScope.selectedSchema = "A"; 设置了默认值,但如果我使用 $scope.selectedSchema = "A"; 它不起作用,同时我无法使用 $rootScope.selectedSchema 和它 returns undefined,但我可以使用 $scope.selectedSchema 读取它,它 returns 选定的值。

对理解这种行为有什么帮助吗?

每次将 $scope 注入控制器时,都会创建新实例(检查 $scope.id)。如果你想在控制器之间传递数据,你应该使用服务。在堆栈溢出时检查此 post。

LINK

根据您的代码,由于您使用的是 $rootScope.selectedSchema = "A"; selectedSchema 是根范围的直接 属性。

每当我们更改 select 框中的 selectedSchema 时,新的 selectedSchema 属性 就会添加到 childScope($scope)。这个新的 属性 hides/shadows parentScope($rootscope) 属性 具有相同的名称。

因此,当您通过分配 $rootScope.selectedSchema = "A"; 来显示 select 字段时,默认显示 'A',但如果您更改一次,新的 $scope.selectedSchema 将被创建并覆盖 $rootScope.selectedSchema,因此您可以使用 $scope.selectedSchema.

进行访问

因此,为了直接更改 rootScope 值,您必须获取一个散列对象并将其键指向该值。

Eg: $rootScope.data = {}
    $rootScope.data.selectedSchema = 'A'

这是我创建的一个例子来说明这个例子。

angular.module('myApp', [])
.run(function($rootScope) {
    
})
.controller('myCtrl', function($scope, $rootScope) {
$rootScope.data = {'selectedSchema': 'A'}
  $scope.changeSchema = function() {
        console.log($scope.data.selectedSchema)
        console.log($rootScope.data.selectedSchema)
    };
    
    $scope.getOrig = function() {
        return $rootScope.data.selectedSchema;
    };
})
.controller('myCtrl2', function($scope, $rootScope) {
$rootScope.selectedSchema = 'A';
    $scope.changeSchema = function() {
        console.log($scope.selectedSchema)
        console.log($rootScope.selectedSchema)
    };
    
       
    $scope.getOrig = function() {
        return $rootScope.selectedSchema;
    };
});
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.min.js"></script>
<div ng-app="myApp">
    
    <div ng-controller="myCtrl">
        <strong>Assiging to an object scope changes rootscope also.</strong>
        <br/>
        <select class="form-control" ng-change="changeSchema();" ng-model="data.selectedSchema">
                        <option value="A">Schema A</option>
                        <option value="B">Schema B</option>
                        <option value="C">Schema C</option>
                        <option value="D">Schema D</option>
                        <option value="E">Schema E</option>
                    </select>
          <br/>
          Rootscope Value: {{getOrig()}}
          <br/>
        
    </div>
      <br/><br/>
      <div ng-controller="myCtrl2">
        <strong>Direct assignmennt.scope will not change rootscope. </strong><br>
         <select class="form-control" ng-change="changeSchema();" ng-model="selectedSchema">
                        <option value="A">Schema A</option>
                        <option value="B">Schema B</option>
                        <option value="C">Schema C</option>
                        <option value="D">Schema D</option>
                        <option value="E">Schema E</option>
                    </select>
          <br/>
          Rootscope Value: {{getOrig()}}
          <br/>
        
      </div>
    </div>

请 运行 比较两种表示。

Here is Js Fiddle.