1

これは HTML (フラグメント) です。

<div class="header" ng-controller="Header" ng-hide="hideHeader"></div>
<div class="page" ng-view></div>

には.header常に同じコントローラがありますが、.pageにはルートに基づいて異なるコントローラがあります。

問題はhideHeader、現在の URL のコントローラーに設定されていることです。

Header現在のルートのコントローラが の値を変更したことをコントローラに伝える最良の方法は何hideHeaderですか?

$routeScopeに設定するのは正しい方法ではないと思います。また、ほとんどの場合、ヘッダーは表示されますが、非表示にしたいページはほとんどありません。

別のアイデアは、その変数をconfig()メソッドで設定することです。

$routeProvider
.when('/',
{
    templateUrl:'views/hello.html',
    controller:'Hello',
    hideHeader:true
});

ただし、それが適切な「AngularJS」の方法であるかどうかはわかりません。

私の最良の選択肢は何ですか?

ありがとうございました。

4

2 に答える 2

-1

$locationChangeStartヘッダー コントローラーでまたはイベントをリッスンし$locationChangeSuccess、URL の変更に基づいて非表示にすることができます。

http://docs.angularjs.org/api/ng .$location

AngularJS API ドキュメントから

$locationChangeStart

Broadcasted before a URL will change. This change can be prevented by calling   preventDefault method of the event. See ng.$rootScope.Scope#$on for more details about  event object. Upon successful change $locationChangeSuccess is fired.

Type:
broadcast

Target: 
root scope

Parameters
Param   Type    Details

angularEvent    Object    Synthetic event object.

newUrl: string    New URL

oldUrl:    (optional)    string    URL that was before it was changed.

編集

angular.module('myApp',[]).config(['$routeProvider',function($routeProvider){
    // application routes here
}).run(['$rootScope','$location',function($rootScope,$location){
    $rootScope.$on('$locationChangeStart',function(evt,newPath,oldPath){
        switch(newPath){
            case '/some/path':
            case '/some/other/path':
            case '/some/more/paths':
                $rootScope.$broadcast('header.hide');
                break;
            default:
                $rootScope.$broadcast('header.show');
                break;
        }
    });
}])
.controller('HeaderCtrlr',['$scope',function($scope){
    $scope.hideFlag = false;

    $scope.$on('header.hide',function(){
        $scope.hideFlag = true;
    });

    $scope.$on('header.show',function(){
        $scope.hideFlag = false;
    });
}]);
于 2013-11-07T18:37:14.720 に答える