简体   繁体   English

使用角度和角度路线扩展动态标题

[英]Extending on dynamic titles using angular and angular-route

I used this answer to get started with setting the title of my application dynamically. 我使用此答案开始动态设置应用程序的标题。 One solution worked nearly perfectly, except in one case. 除一种情况外,一种解决方案几乎可以完美工作。 The following is my router: 以下是我的路由器:

app.config(['$routeProvider', function($routeProvider) {
        $routeProvider.when('/', {
            templateUrl: '/partials/home.html',
            controller: 'home',
            title: "Corvid"
        })
        .when('/archive', {
            templateUrl: "/partials/archive/all.html",
            controller: "archive",
            title: "Archive"
        }).
        when('/archive/:id', {
            templateUrl: "/partials/archive/one.html",
            controller: "article",
            title: ""
        })
        .otherwise({
            templateUrl: "/partials/errors/404.html",
            title: "Something went wrong!"
        });
    }
]);

app.run(['$location', '$rootScope', 
    function($location, $rootScope) {
        $rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
            $rootScope.title = current.$$route.title;
        });
    }
]);

When using pre-defined and concrete strings, like "Corvid", it will work perfectly well enough. 当使用预定义的具体字符串(例如“ Corvid”)时,它将可以很好地工作。 However, for the case of /archive/:id , I want something more arbitrary; 但是,对于/archive/:id ,我想要更随意的东西。 the title of the article . 文章的标题 That is to say, #/archive/2/ would set the title to Breaking news; 也就是说, #/archive/2/会将title设置为Breaking news; corvid sucks at angular.js . corvid很讨厌angular.js Here is my factory for article: 这是我的工厂文章:

services.factory('Article', ['$resource', function($resource) {
        return $resource('/api/v1/articles/:articleId', {}, {
            query: { method: 'GET', params: { articleId: '' } }
        });
    }
]);

So when I grab one of these articles, I want the title attribute to be whatever the title attribute is of that resource 因此,当我抓取其中一篇文章时,我希望title属性可以是该资源的title属性

You could change your title property into a method, as follows: 您可以将title属性更改为方法,如下所示:

app.config(['$routeProvider', function($routeProvider) {
        $routeProvider.when('/', {
            templateUrl: '/partials/home.html',
            controller: 'home',
            title: function () { return "Corvid"; }
        })
        .when('/archive', {
            templateUrl: "/partials/archive/all.html",
            controller: "archive",
            title: function () { return "Archive"; }
        }).
        when('/archive/:id', {
            templateUrl: "/partials/archive/one.html",
            controller: "article",
            title: function (Article) { return Article.get(this.id).title; }
        })
        .otherwise({
            templateUrl: "/partials/errors/404.html",
            title: function () { return "Something went wrong!"; }
        });
    }
]);

Then call it later, and pass the injected Article service. 然后再调用它,并通过注入的Article服务。

app.run(['$location', '$rootScope', 'Article',
    function($location, $rootScope, Article) {
        $rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
            $rootScope.title = current.$$route.title(Article);
        });
    }
]);

I think you would be best of using resolve in your route. 我认为您最好在自己的路线中使用解决方案。 You can use it to get the article from your service and set the title at the same time. 您可以使用它从服务中获取文章并同时设置标题。 Example of using resolve: 使用resolve的示例:

resolve = {
    article: ['$route', 'Service', function($route, Service) {
        var id = $route.current.params.id;
        return Service.get({'id': id}).$promise.then(function(article) {
            $route.current.$$route.title = article.id;
            return article;
        });
    }]
}

Now you don't have to fetch the article in your controller, you can simply inject article and at the same time you've set the title property in your route so your routechangehandler will simply work as expected. 现在,您不必在控制器中获取文章,只需插入article ,并同时在路径中设置title属性,以便routechangehandler即可按预期工作。

From the API docs: 从API文档:

resolve - {Object.=} - An optional map of dependencies which should be injected into the controller. resolve-{Object。=}-可选的依赖关系映射,应将其注入到控制器中。 If any of these dependencies are promises, the router will wait for them all to be resolved or one to be rejected before the controller is instantiated. 如果这些依赖关系中的任何一个是应许的,路由器将在实例化控制器之前等待所有这些依赖关系被解决或一个被拒绝。 If all the promises are resolved successfully, the values of the resolved promises are injected and $routeChangeSuccess event is fired. 如果所有的诺言都已成功解决,则将注入已解决的诺言的值,并触发$ routeChangeSuccess事件。

See https://docs.angularjs.org/api/ngRoute/provider/$routeProvider 参见https://docs.angularjs.org/api/ngRoute/provider/$routeProvider

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM