有没有一种方法可以在抽象状态之间进行,以避免重复相同的视图模板细节?

时间:2016-05-14 07:43:40

标签: angularjs angular-ui-router

我对angular-ui-router有这个配置:

{{1}}

我有一个编辑,删除和一个所有共享相同“views:”的新状态。有没有办法可以让某种抽象状态只包含视图定义,这样我每次都可以避免重复“视图”?

2 个答案:

答案 0 :(得分:1)

尝试以下

var viewDes= {
            "wordData@r.s.s.a.w.w": {
                templateProvider: ['$templateCache', ($templateCache) => {
                    return $templateCache.get('/app/admin/word/wordData.html');
                }]
            },
            "wordForms@r.s.s.a.w.w": {
                templateProvider: ['$templateCache', ($templateCache) => {
                    return $templateCache.get('/app/admin/word/wordForms.html');
                }]
            }
        };
var rootSubjectsSubjectAdminWordsWordDelete = {
        name: 'r.s.s.a.w.w.delete',
        views: viewDes,
        url: '/delete',
    };

    var rootSubjectsSubjectAdminWordsWordEdit = {
        name: 'r.s.s.a.w.w.edit',
        views: viewDes,
        url: '/edit',
    };

答案 1 :(得分:1)

您可以使用视图配置创建共享变量:

var sharedViewConfig = {
    "wordData@r.s.s.a.w.w": {
        templateProvider: ['$templateCache', ($templateCache) => {
            return $templateCache.get('/app/admin/word/wordData.html');
        }]
    },
    "wordForms@r.s.s.a.w.w": {
        templateProvider: ['$templateCache', ($templateCache) => {
            return $templateCache.get('/app/admin/word/wordForms.html');
        }]
    }
};

var rootSubjectsSubjectAdminWordsWordDelete = {
    name: 'r.s.s.a.w.w.delete',
    views: sharedViewConfig,
    url: '/delete',
};

var rootSubjectsSubjectAdminWordsWordEdit = {
    name: 'r.s.s.a.w.w.edit',
    views: sharedViewConfig,
    url: '/edit',
};

或者您可以创建一个抽象状态:

var abstractState = {
    name: 'r.s.s.a.w.w.abstract',
    abstract: true,
    views: {
        "wordData@r.s.s.a.w.w": {
            templateProvider: ['$templateCache', ($templateCache) => {
                return $templateCache.get('/app/admin/word/wordData.html');
            }]
        },
        "wordForms@r.s.s.a.w.w": {
            templateProvider: ['$templateCache', ($templateCache) => {
                return $templateCache.get('/app/admin/word/wordForms.html');
            }]
        }
    }
};

var rootSubjectsSubjectAdminWordsWordDelete = {
    name: 'r.s.s.a.w.w.delete',
    parent: 'r.s.s.a.w.w.abstract',
    url: '/delete',
};

var rootSubjectsSubjectAdminWordsWordEdit = {
    name: 'r.s.s.a.w.w.edit',
    parent: 'r.s.s.a.w.w.abstract',
    url: '/edit',
};