Angular單頁面動態修改title[兼容微信]

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的變化來動態調用標題html

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 動態調用titleapp

<head>
    <title ng-bind="title">首頁</title>
</head>
相關文章
相關標籤/搜索