Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用AngularJs在控制器之间共享$scope我们可以访问一个控制器吗’;从另一个控制器获取s$范围?_Javascript_Angularjs_Ng Controller_Ng App - Fatal编程技术网

Javascript 如何使用AngularJs在控制器之间共享$scope我们可以访问一个控制器吗’;从另一个控制器获取s$范围?

Javascript 如何使用AngularJs在控制器之间共享$scope我们可以访问一个控制器吗’;从另一个控制器获取s$范围?,javascript,angularjs,ng-controller,ng-app,Javascript,Angularjs,Ng Controller,Ng App,我们可以从另一个控制器访问一个控制器的$scope吗? 我需要在父、子和孙控制器中获取全部数据,是否可能 这是我的剧本 var app = angular.module('myApp', []); app.controller('ParentController',ParentController); function ParentController($scope) { $scope.parentName="Parent" } app.controller('ChildControl

我们可以从另一个控制器访问一个控制器的$scope吗? 我需要在父、子和孙控制器中获取全部数据,是否可能

这是我的剧本

var app = angular.module('myApp', []);

app.controller('ParentController',ParentController);
function ParentController($scope)
{
    $scope.parentName="Parent"
}
app.controller('ChildController1',ChildController1);
function ChildController1($scope)
{
    $scope.childName1="Child1"
}
app.controller('ChildController2',ChildController2)
function ChildController2($scope)
{
    $scope.childName2="Child2"
}
app.controller('GrandChildController1',GrandChildController1);
function GrandChildController1($scope)
{
    $scope.grandChildName1="GrandChild1"
}
这是我的查看代码

<div>  ng-app="myApp">

<div ng-controller="ParentController">

    Parent:{{parentName}}
    Child1:{{childName1}}
    Child2:{{childName2}}
    GrandChild1:{{grandChildName1}}

    <div ng-controller="ChildController1">

        Parent:{{parentName}}
        Child1:{{childName1}}
        Child2:{{childName2}}
        GrandChild1:{{grandChildName1}}

        <div ng-controller="GrandChildController1">     

            Parent:{{parentName}}
            Child1:{{childName1}}
            Child2:{{childName2}}
            GrandChild1:{{grandChildName1}} 

        </div>

    </div>

    <div ng-controller="ChildController2">

        Parent:{{parentName}}
        Child1:{{childName1}}
        Child2:{{childName2}}
        GrandChild1:{{grandChildName1}}

    </div>  
</div>

</div>
ng app=“myApp”>
父项:{{parentName}
Child1:{{childName1}
Child2:{{childName2}
孙子1:{{孙子名称1}}
父项:{{parentName}
Child1:{{childName1}
Child2:{{childName2}
孙子1:{{孙子名称1}}
父项:{{parentName}
Child1:{{childName1}
Child2:{{childName2}
孙子1:{{孙子名称1}}
父项:{{parentName}
Child1:{{childName1}
Child2:{{childName2}
孙子1:{{孙子名称1}}

我没有在父控制器中获取子作用域。我该怎么做。请编写任何服务或其他内容。谢谢您

简单
$scope
将控制器嵌套到您的视图中可以继承

<div ng-controller="a">
  {{num}}
  <div ng-controller="b">
    {{num}}
  </div>
</div>

您也可以始终使用在控制器之间传递数据,通常使用
$broadcast
$emit

欢迎使用stackoverflow。在控制器之间共享数据是一个非常常见的问题,正确的答案通常是使用服务(编写一个服务来保存要在控制器之间共享的数据)。您可以从子级访问父级子级,但不能从相反的子级访问父级子级。创建一个服务以在控制器和模块之间共享数据(比rootscope更好),所以很多人告诉我避免使用rootscope而使用服务。我是Angular js的新手。如何在这里创建服务。给我一个代码示例。谢谢。这个问题应该有帮助:如何使用服务访问不同的控制器
app.controller('a', a);
function a($scope) {
  $scope.num = 1
}


app.controller('b', b);
function b($scope) {
    //$scope.num is inherited by the parent controller
}