加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular单页面动态修改title[兼容微信]

发布时间:2020-12-17 09:20:09 所属栏目:安全 来源:网络整理
导读:js文件 定义module var app = angular.module('app',['ngRoute']); 在config通过路由定义标题 app.config(['$routeProvider','$locationProvider',function ($routeProvider,$locationProvider) { $routeProvider .when('/index',{templateUrl: '../tpls/hom

js文件

定义module

var app = angular.module('app',['ngRoute']);

在config通过路由定义标题

app.config(['$routeProvider','$locationProvider',function ($routeProvider,$locationProvider) {
        $routeProvider
            .when('/index',{templateUrl: '../tpls/home.html',title: '首页'})
            .otherwise({redirectTo: '/index',title: '首页'});
    }]);

通过run动态调用标题

在里面定义run,通过监听 $routeChangeSuccess的变化来动态调用标题

app.run(['$rootScope',function ($rootScope) {
       
        $rootScope.$on('$routeChangeSuccess',function (event,current,previous) {
            $rootScope.title = current.$$route.title || '首页';
            document.title = current.$$route.title || '首页';
            var $body = angular.element('body');
            var $iframe = $('<iframe src="../image/arrow.png" style="display: none"></iframe>').on('load',function () {
                $timeout(function () {
                    $iframe.off('load').remove();
                },0);
            }).appendTo($body);

        });

HTML 文件

在html里同过头部head里的title 动态调用title

<head>
    <title ng-bind="title">首页</title>
</head>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读