各国之间共享$范围数据 [英] Share $Scope data between States

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

问题描述

我从一个孩子试图访问父状态。我试过,但它不能正常工作。

  angular.module('对myApp')。控制器('compareCtrl',['$范围',
    功能($范围){
 。$ scope.test = $ $范围parent.services;

app.coffee

 
  .module('对myApp',['ngAnimate','ngCookies','ngResource'
                  'ui.router','ngSanitize','ngTouch'])
  的.config($ stateProvider) - GT;
    $ stateProvider.state(家,
      网址:/
      templateUrl:home.html做为
    ).STATE('服务',
      网址:/服务
      templateUrl:服务/ list.html
    ).STATE('services.detail',
      URL:/细节/
      templateUrl:detail.html
    ).STATE('services.compare',
      URL:/比较/
      templateUrl:compare.html
    )


解决方案

UI-路由器支持国家家庭中的数据(模型)共享。详细的说明可以在这里找到。

如何在angularjs UI路由器状态之间共享$范围数据?

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

  //控制器父状态服​​务的
.controller('ServicesCtrl',['$范围',
    功能($范围){
        $ scope.Model = {为prop1:值1,...};
}])

由于现在每个孩子国家将中典型继承参考$ scope.Model ......我们可以在任何子状态控制器访问

  .controller('ServiceChildCtrl',['$范围',
    功能($范围){
        $ scope.Model.prop1 = differentValue;
}])

检查它的行动在这个工作plunker

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 supports data (Model) sharing among state families. The detailed explanation could be found here

How do I share $scope data between states in angularjs ui-router?

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, ...};
}])

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;
}])

Check it in action in this working plunker

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆