使用ui-router时控制器可以从父控制器继承范围吗控制器、范围、ui、router

2023-09-06 08:56:53 作者:那丶泪

我有以下几点:

var admin = {

   name: 'admin',
    url: '/admin',
    views: {
        'nav-sub': {
            templateUrl: '/Content/app/admin/partials/nav-sub.html',
            controller: function ($scope) { $scope.message = "hello"; }
        }
    },
    controller: ['$scope', function ($scope) {
        $scope.message = "hello";
    }]
}

var subject = {
    name: 'subject',
    parent: admin,
    url: '/subject',
    views: {
        'grid@': {
            templateUrl: '/Content/app/admin/partials/grid-subject.html',
            controller: 'AdminGridSubjectController',
        }
    }
};

我希望 AdminGridSubjectController 知道 $scope.message 值是什么,但它似乎对此一无所知.是不是我做错了什么?

I would like the AdminGridSubjectController to know what the $scope.message value is but it seems not to know anything about it. Is there something I am doing wrong?

stApp.controller('AdminGridSubjectController', ['$scope', function ( $scope ) {
    var a = $scope.message;
}]);

推荐答案

为了在 Angular UI Router 中访问父控制器的 scope,请使用:

In order to access the scope of a parent controller in Angular UI Router use:

$scope.$parent

然后,您可以免费使用父范围.

Then the parent scope is then freely available to you.