Angular ui-router:保持子级相同的ui-view

IT小君   2023-05-12T00:41:12

我需要子状态能够使用父状态的resolve函数。但我也需要保持相同的ui-view用于两个状态。这是一个fiddle。这是代码:

    $stateProvider
        .state('parent', {
            url: "/",
            template: '<p>你好{{parent.one}}</p><br>'
                    + '<button ng-click="goToChild()">子</button><br>',
            // 下面这个可以,但我不需要它
            // template: '<p>Hello {{parent.one}}</p><br>'
            //      + '<button ng-click="goToChild()">child</button><br>'
            //      + '<div ui-view></div>',
            resolve: {
                test: function() {
                    return 1;
                }
            },
            controller: function($scope, $state, test) {
                $scope.parent = { one: test };
                $scope.goToChild = function() {
                    $state.go('parent.child');
                }
            }
        })
        .state('parent.child', {
            url: "/child",
            template: '<p>你好{{child.one}}</p>',
            controller: function($scope, test) {
                $scope.child = { one: test };
            }
        })
    $urlRouterProvider.otherwise('/');
点击广告,支持我们为你提供更好的服务
评论(1)