2016-09-06 71 views

回答

2

試試這個:

(function() { 
    'use strict'; 

    angular 
     .module('app', ['ui.router']) 
     .config(config) 

    config.$inject = ['$stateProvider', '$urlRouterProvider', '$urlMatcherFactoryProvider']; 

    function config($stateProvider, $urlRouterProvider, $urlMatcherFactoryProvider) { 

     $urlRouterProvider.otherwise('/home'); 

     $urlMatcherFactoryProvider.caseInsensitive(true); 

     $stateProvider 
      .state('home', { 
       url: '/home', 
       templateUrl: 'home.view.html', 
       data: { 
        pageTitle: 'Home' 
       } 
      }) 
      .state('about', { 
       url: '/about', 
       templateUrl: 'about.view.html', 
       data: { 
        pageTitle: 'About' 
       } 
      }) 

    } 



})(); 

<!DOCTYPE html> 
<html ng-app="app"> 

    <head> 
    <meta charset="utf-8" /> 
    <script src="https://code.angularjs.org/1.3.3/angular.js"></script> 
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.11/angular-ui-router.min.js"></script> 

    <script src="app.js"></script> 
    <script src="app-directives/title-directive.js"></script> 
    <title>{{title}}</title> 
    </head> 

    <body> 
    <div class="panel" ui-view></div> 
    <ul class="nav navbar-nav"> 
     <li> 
     <a ui-sref="home" ui-sref-active="activeState">Home</a> 
     </li> 
     <li> 
     <a ui-sref="about">About</a> 
     </li> 
    </ul> 
    </body> 

</html> 

https://plnkr.co/edit/JifcOKrUC9tBfV8jY5ko?p=preview

1

在plunkr你就必須通過

<script src="title-directive.js"></script> 

我可以爲您提供一個更好的方式來做到這一點,以取代

<script src="app-directives/title-directive.js"></script> 

link: function (scope, element, attrs) { 
        var defaultTitle = element.text(); 
        if (element[0].tagName === 'TITLE') { 
         var listener = function (event, toState) { 
          var title; 
          if (toState.data && toState.data.pageTitle) { 
           if(toState.data.placeholder && toState.data.placeholder.title) 
            title = toState.data.pageTitle, toState.data.placeholder.title; 
           else 
            title = toState.data.pageTitle; 
          } else if (defaultTitle) { 
           title = defaultTitle; 
          } else { 
           title = 'No title'; 
          } 

          $timeout(function() { 
           element.text(title); 
          }, 0, false); 
         }; 

         $rootScope.$on('$stateChangeSuccess', listener); 
        } 
       } 
0

由於問題解決 有是使用在線版本時ui路由器腳本中的錯誤,並且版本不兼容 問題解決了

謝謝全部