angularjs
  • internet-explorer-11
  • 2016-06-16 27 views 2 likes 
    2

    我知道這已被問了很多次。實際上任何解決方案在Chrome中都可以100%正常工作,但IE是另一回事。下面是我用一個非常簡單的代碼: 主頁:Angular環境中的控件頁標題

    <title ng-bind="title"></title> 
    

    控制器:

    $rootScope.$on('$stateChangeSuccess', function() { 
        //$window.document.title = 'Prism Main'; 
        $rootScope.title = 'Main Page'; 
    }); 
    

    IE所說:主頁#/ Statename的

    任何想法,爲什麼它不工作IE瀏覽器?

    謝謝

    +0

    似乎對我的IE-11 – gaurav5430

    回答

    0

    您可以在級別定義控制器。

    <html ng-app="app" ng-controller="titleCtrl"> 
        <head> 
        <title>{{ Page.title() }}</title> 
    

    ... 創建服務:頁面和控制器修改。

    myModule.factory('Page', function() { 
        var title = 'default'; 
        return { 
        title: function() { return title; }, 
        setTitle: function(newTitle) { title = newTitle } 
        }; 
    }); 
    

    從控制器注入頁面和調用'Page.setTitle()'。

    這裏是一個plunker http://plnkr.co/edit/0e7T6l?p=preview

    +0

    工作,我認爲這將拿出{{Page.title ()}}在狀態改變之前。 – Mark

    +0

    你需要從所有控制器調用此服務...所以你可以定製它.. –

    0

    這可以很容易的方式,是跨瀏覽器的工作來完成。

    您可能希望創建一個指令,您可以將其放入頁面:<some-element page-title="My Page Title"></some-element>或使用服務。我將演示一個簡單的服務。

    PageTitle.$inject = ['$document']; 
    function PageTitle($document) { this.$document = $document } 
    PageTitle.prototype.setPageTitle = function(title) { 
        this.$document.title = title; 
    } 
    
    app.service('pageTitle', PageTitle); 
    

    然後在其他地方,你有這樣的服務注入並使用它簡稱爲:

    pageTitle.setPageTitle("Foo"); 
    
    相關問題