本文介绍了在州之间共享 $Scope 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从孩子访问父状态.我试过了,但没有用.

I am trying access the parent state from a child. I tried this but it doesn't work.

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-Router 支持状态族之间的数据(模型)共享.详细解释可以在这里找到

UI-Router supports data (Model) sharing among state families. The detailed explanation could be found here

如何在 angularjs ui-router 中的状态之间共享 $scope 数据?

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

Where we can see, that we need to introduce a Model, a cluster, a reference object.

// controller of the parent state 'services'
.controller('ServicesCtrl', ['$scope',
    function($scope) {
        $scope.Model = { prop1 : value1, ...};
}])

因为现在每个子状态都会原型继承对 $scope.Model 的引用...我们可以在任何子状态控制器中访问它

Because now each child state will prototypically inherit that reference to $scope.Model... we can access it in any child state controller

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

这个工作plunker

这篇关于在州之间共享 $Scope 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 03:24