javascript - Angular ui 路由器 : Child not getting Parent state

标签 javascript angularjs angular-ui-router

我有一个使用 ui-router 的 Angular 应用程序,并在我的应用程序中定义了以下状态。

$stateProvider
  .state('classes', {
    url: '/classes/:type',
    templateUrl: 'classes.html',
    controller: 'ClassesCtrl',
  })
  .state('classes.detail', {
    url: '/:id',
    views: {
        "@": {
            templateUrl: 'class.html',
            controller: 'ClassCtrl'
        }
    }
  });

我的 Controller 看起来像:

app.controller('ClassesCtrl', function($scope, $stateParams){
  $scope.foo = "bar";
});
app.controller('ClassCtrl', function($scope, $stateParams){
  console.log($scope.foo);
});

如您所见,class.detail 通过定位根级别未命名的 ui-view 来隐藏父 View 。我遇到的问题是子级没有继承父级的范围(我无法访问 class.detail 中的 $scope.foo )。当我进入子状态时,父状态似乎被破坏了,因为如果我单击返回,它必须重新加载所有数据。

如何有效隐藏父 View 但仍访问父数据?

最佳答案

来自 ui-router 文档:

Scope Inheritance by View Hierarchy Only

Keep in mind that scope properties only inherit down the state chain if the views of your states are nested. Inheritance of scope properties has nothing to do with the nesting of your states and everything to do with the nesting of your views (templates).

好吧,您使用 @ 来定义 View ,但您的父状态已命名,因此您应该将其命名为 @classes

编辑: I made this fiddle解释名称。

关于javascript - Angular ui 路由器 : Child not getting Parent state,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32704747/

相关文章:

javascript - 对于 Chrome 扩展,从 https 站点调用 http 站点时出现错误

javascript - 为什么除非我提供 onChange 回调,否则 React 会使 input[type ="text"] 字段只读?

javascript - 使用 javascript 调用 Angular js

javascript - 初学者的 AngularJS 路由

javascript - ui-router $transitions 钩子(Hook)在浏览器重新加载时不会被调用

javascript - 隐藏元素,1秒延迟

当ng-model在 Controller 中初始化时,Angularjs select ng-change不会被触发

javascript - 如何在 for 循环中每次强制异步完成,并修改全局变量?

angularjs - Angular UI 路由器 $transitions.onBefore

javascript - 如何使用 angularjs 包含脚本