【问题标题】:Page title, description using angular-ui-router页面标题,使用 angular-ui-router 的描述
【发布时间】:2016-12-26 05:40:25
【问题描述】:

我已经尝试/使用了 stackoverflow 和其他教程中所有可能的参考资料,但无法获得页面标题和说明。 我的状态码是这样的

     $urlRouterProvider.otherwise('/home/welcome');
      $stateProvider
        .state('home', {
          url: "/home",
          abstract: true,
          templateUrl: "views/home_base.html",
          controller : "HomebaseCtrl"
        })

        .state('home.welcome', {
          url: '/welcome',
          templateUrl: 'views/welcome.html',
          controller: 'WelcomeCtrl'
        })

在我的控制器“WelcomeCtrl”文件中,我想为页面设置页面标题和描述。我使用了 rootScope,但 rootScope 值没有绑定到 index.html 文件中。

在我的 index.html 文件中

  <html>
  <head>
   <title>{{pagetitle}}</title>
  </head>
  <div ui-view=""></div>

路由 home_base.html 文件,该文件返回路由welcome.html 文件。

控制器

angular.module('ragamixApp')
  .controller('WelcomeCtrl', function($rootScope){
    $rootScope.pagetitle = "Hi..I am Page Title";
   })

任何帮助将不胜感激

【问题讨论】:

  • 你的控制器?代码?更新以发布
  • 您正在尝试绑定应用程序之外的元素。如果你想访问{{pagetitle}},它必须在一个受控的dom元素下。
  • 你的控制器在哪里?
  • 我认为您缺少 ng-app 指令。将ng-app="ragamixApp" 指令放在你的html标签中。
  • @UmairAhmed - 你杀了它兄弟!谢谢

标签: javascript angularjs angular-ui-router


【解决方案1】:

我认为您缺少 ng-app 指令。
ng-app="ragamixApp" 指令放在你的html标签中。

https://docs.angularjs.org/api/ng/directive/ngApp

【讨论】:

    【解决方案2】:

    你试试这个。

    如果您查看 ui-router 示例应用程序,他们会使用角度 .run 块将 $state 变量添加到 $rootScope。

    .run([ '$rootScope', '$state', '$stateParams',
    function ($rootScope, $state, $stateParams) {
      $rootScope.$state = $state;
      $rootScope.$stateParams = $stateParams;
    }])
    

    以同样的方式设置状态:

    .state('home', {
        url: '/home',
        templateUrl : 'views/home.html',
        data : { pageTitle: 'Home' }
    })
    

    HTML

    <title ng-bind="$state.current.data.pageTitle"></title>
    

    【讨论】:

    • 我已经尝试过了,但没有运气。我使用嵌套的
      有问题吗?我的意思是在 index.html 文件中我使用了
      来路由我的抽象状态,然后在那个 html 文件中我再次使用了
      路由welcome.html 内容?我也在使用 angular 1.6
    猜你喜欢
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多