使2个州分享决心的一部分

时间:2017-07-03 02:12:29

标签: javascript angularjs angular-ui-router nested-routes nested-views

我已经定义了两种状态:

app.config(['$stateProvider', function ($stateProvider) {
    $stateProvider
        .state('edit', {
            url: '/edit/{id}',
            templateUrl: '/htmls/h1.html',
            controller: 'SameCtrl',
            onEnter: ...sameOnEnter...
            resolve: {
                ...commonResolve...
            }
        })
        .state('addinEdit', {
            url: '/addin/edit/{id}',
            templateUrl: '/htmls/h2.html',
            controller: 'SameCtrl',
            onEnter: ...sameOnEnter...
            resolve: {
                special: [ ... ],
                ...commonResolve...
            }
        })
}])

所以他们共享相同的控制器,相同的onEnter,他们有一个很长resolve的共同部分(实际上是链< / strong> of resolves:first: function (...){...}, second: function (...){...}, ...)。有谁知道如何重写它们以便我不必写两次commonResolve

2 个答案:

答案 0 :(得分:1)

只需为解析器创建一个函数:

    app.config(['$stateProvider', function ($stateProvider) {

        resolverFunction.$inject = ['resolverA', 'resolverB'];
        function ResolverFunction(myService1, myService2) {
            return 'something';
        }

        resolverAFunction.$inject = ['resolverC'];
        function resolverAFunction(resolverC) {
            return 'anything';
        }

        resolverBFunction.$inject = ['resolverC'];
        function resolverBFunction(resolverC) {
            return 'something else';
        }

        resolverCFunction.$inject = ['service'];
        function resolverCFunction(service) {
            return 'something else';
        }


        $stateProvider
            .state('edit', {
                url: '/edit/{id}',
                templateUrl: '/htmls/h1.html',
                controller: 'SameCtrl',
                onEnter: ...sameOnEnter...
                resolve: {
                   commonResolver: resolverFunction,
                       resolverA: resolverAFunction,
                       resolverB: resolverBFunction,
                       resolverC: resolverCFunction,

                }
    })
        .state('addinEdit', {
            url: '/addin/edit/{id}',
            templateUrl: '/htmls/h2.html',
            controller: 'SameCtrl',
            onEnter: ...sameOnEnter...
            resolve: {
               special: [ ... ],
               commonResolver: resolverFunction,
                resolverA: resolverAFunction,
                resolverB: resolverBFunction,
                resolverC: resolverCFunction,

            }
    })
    }])

答案 1 :(得分:0)

我没有经验angularjs但我找到了解决方案, 您可以通过父属性指定州的父级。

app.config(['$stateProvider', function ($stateProvider) {
$stateProvider
    .state('edit', {
        url: '/edit/{id}',
        templateUrl: '/htmls/h1.html',
        controller: 'SameCtrl',
        onEnter: ...sameOnEnter...
        resolve: {
            ...commonResolve...
        }
    })
    .state('addinEdit', {
        url: '/addin/edit/{id}',
        templateUrl: '/htmls/h2.html',
        parent : 'edit'
    })
}])