从Ctrl中更改导航栏中的标题

时间:2015-11-13 17:37:33

标签: angularjs ionic

您好我的应用程序中对离子的新手我想要做的是根据用户所在的页面更改应用页面的标题。例如,如果用户在主页上,如果他们在另一个页面上,则应该在顶部说主页,它应该在顶部说另一个

每个页面都有自己的控制器,我为我的应用程序创建了一个名为AppCtrl的父控制器,并将其放在窗格上:

<ion-pane ng-controller="AppCtrl">
</ion-pane>

这是我的AppCtrl:

 // Root Controller
        .controller("AppCtrl", function($rootScope, $scope) {

            // Adding Sum Property To Array Prototype
            Array.prototype.sum = function (prop) {
                var total = 0
                for (var i = 0, _len = this.length; i < _len; i++) {
                    total += this[i][prop]
                }
                return total
            }

            $scope.title = {
                name: '',
                icon: ''
            };

            // Function To Change Pane Title
            $rootScope.changeTitle = function(name, icon) {

                $rootScope.title = {
                    name: name,
                    icon: icon
                };

                $rootScope.$broadcast('TITLE_UPDATED', $rootScope.title);

            }

            $scope.$on('TITLE_UPDATED', function(event, title) {
                $scope.title = {
                    name: title.name,
                    icon: title.icon
                };
            })

        })

这是我的家庭控制器:

 // Home View Controller
        .controller("HomeCtrl", function($scope, $rootScope) {

            // Setting Title
            $scope.changeTitle('Home', 'ion-home');

        })

这是我的index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>


    <!-- compiled css output -->
    <link href="css/ionic.app.css" rel="stylesheet">

    <!-- custom css styles -->
    <link href="css/style.css" rel="stylesheet">

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
    <script src="js/app.ctrls.js"></script>
</head>
<body ng-app="mtgh">

<ion-pane ng-controller="AppCtrl">
    <!-- </header + navbar> -->
    <ion-side-menus enable-menu-with-back-views="false">
        <ion-side-menu-content>
            <ion-nav-bar class="bar-stable bar-header-shadow has-header">
                <ion-nav-back-button>
                </ion-nav-back-button>

                <ion-nav-buttons side="left">
                    <button class="button button-icon button-clear ion-navicon" menu-toggle="left">
                    </button>

                    <h1 class="title">
                        <i class="{{ title.icon }}"></i> {{ title.name }}
                    </h1>
                </ion-nav-buttons>
            </ion-nav-bar>

            <ion-nav-view name="content">
            </ion-nav-view>
        </ion-side-menu-content>

        <h1 class="title">
            <img src="img/logo-94x35.png" alt="Starter" title="Starter">
        </h1>

        <ion-side-menu side="left">
            <ion-header-bar class="bar-stable">
                <h1 class="title">
                    <img src="img/logo-94x35.png" alt="Starter" title="Starter">
                </h1>
            </ion-header-bar>
            <ion-content>
                <ion-list>
                    <ion-item menu-close="" ng-click="login()">
                        Login
                    </ion-item>
                    <ion-item menu-close="" href="#/app/search">
                        Search
                    </ion-item>
                    <ion-item menu-close="" href="#/app/browse">
                        Browse
                    </ion-item>
                    <ion-item menu-close="" href="#/app/playlists">
                        Playlists
                    </ion-item>
                </ion-list>
            </ion-content>
        </ion-side-menu>
    </ion-side-menus>
    <!-- </header> -->

    <!-- <footer> -->
    <ion-footer-bar class="bar-stable">
        <h1 class="title">Starter</h1>
    </ion-footer-bar>
    <!-- </footer> -->

</ion-pane>

</body>
</html>

1 个答案:

答案 0 :(得分:2)

您可以将changeTitle函数设置为在run方法中生效,以便进行全局设置。以下的plunker只使用title.name来显示它,你只需要在主导航中替换你的标题元素。

http://plnkr.co/edit/Nx9eLhYq62COLaFd21qT?p=preview

你也会注意到我删除了控制器中的局部范围标题引用,这是因为AngularJS中的范围是分层的,如果它在当前子范围内找不到它将会看起来链上,在这种情况下直接到rootScope。

参考代码:

app.js:

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

app.config(['$routeProvider', function($routeProvider){
  $routeProvider.
  when('/', {
    controller: 'MainCtrl',
    template: '<div><a href="#/home">Go Home</a></div>'
  }).
  when('/home', {
    controller: 'HomeCtrl',
    template: '<div>At Home <a href="#/">Go Back</a></div>'
  })
}]);

app.run(['$rootScope',function($rootScope){
  $rootScope.changeTitle = function(name, icon) {
    $rootScope.title = {
      name: name,
      icon: icon
    };
  } 
}]);

app.controller('MainCtrl', function($scope) {
  $scope.changeTitle('Main', 'ion-settings');
});

app.controller('HomeCtrl', function($scope, $rootScope){
  $scope.changeTitle('Home', 'ion-home');
})

的index.html:

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

  <head>
    <meta charset="utf-8" />
    <title>{{title.name}}</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.4.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.js" data-semver="1.4.7"></script>
    <script data-require="angular-route@1.4.6" data-semver="1.4.6" src="https://code.angularjs.org/1.4.6/angular-route.js"></script>
    <script src="app.js"></script>
  </head>

  <body>
    <h1>{{title.name}}</h1>
    <div ng-view></div>
  </body>

</html>