Angularjs 在州之间共享$Scope数据

Angularjs 在州之间共享$Scope数据,angularjs,angularjs-scope,angular-ui-router,Angularjs,Angularjs Scope,Angular Ui Router,我正在尝试从子级访问父级状态。我试过了,但没用 angular.module('myApp').controller('compareCtrl', ['$scope', function($scope) { $scope.test=$scope.$parent.services; app.coffee angular .module('myApp', ['ngAnimate', 'ngCookies', 'ngResource' , 'ui.r

我正在尝试从子级访问父级状态。我试过了,但没用

angular.module('myApp').controller('compareCtrl', ['$scope',
    function($scope) {
 $scope.test=$scope.$parent.services;
app.coffee

angular
  .module('myApp', ['ngAnimate', 'ngCookies', 'ngResource'
                  , 'ui.router', 'ngSanitize', 'ngTouch'])
  .config ($stateProvider) ->
    $stateProvider.state('home',
      url: "/"
      templateUrl: "home.html"
    ).state('services',
      url: "/services"
      templateUrl: "services/list.html"
    ).state('services.detail',
      url: "/detail/"
      templateUrl: "detail.html"
    ).state('services.compare',
      url: "/compare/"
      templateUrl: "compare.html"
    )
UI路由器支持状态族之间的数据(模型)共享。详细的解释可以在这里找到

我们可以看到,我们需要引入一个模型,一个集群,一个引用对象

// controller of the parent state 'services'
.controller('ServicesCtrl', ['$scope',
    function($scope) {
        $scope.Model = { prop1 : value1, ...};
}])
因为现在每个子状态将典型地继承对$scope.Model的引用。。。我们可以在任何子状态控制器中访问它

.controller('ServiceChildCtrl', ['$scope',
    function($scope) {
        $scope.Model.prop1 = differentValue;
}])

检查它在

中的作用共享数据应该在我的朋友的服务中。您永远不知道实际数据将驻留在范围层次结构的哪个级别。或者它可能完全处于不同的层次结构中。