0

我想确保isSearchVisible范围变量总是false在每次页面更改时开始。我应该如何实施?

app.controller('MainController', function($rootScope) {
  $rootScope.isSearchVisible = false;
});
    
app.controller('ProfileController', function($scope) {
  $scope.isSearchVisible = true;
});
    
app.controller('AboutUsController', function($scope) {

});

在每个页面中,根范围变量isSearchVisiblefalse因为MainController.

当您进入配置文件页面 ( ProfileController) 时,本地范围变量变为true,这很好。

但是当您离开此页面时,根范围变量也更改为true. $rootScope变量和变量之间没有分离$scope

除非控制器直接更改它,否则我应该如何在每次页面更改时重置isSearchVisiblefalse

4

2 回答 2

0

将事件侦听器添加到根范围,然后做你想做的事

$rootScope.$on('$routeChangeStart', function(next, current) { ... check your url if you want to show the search box in that url make $rootScope.showSearchBlock = true else make it false. and remove other instances of the $rootScope.showSearchBlock put this on your main controller ... });

于 2014-11-03T11:58:09.680 回答
0

您必须使用服务来共享数据:

app.factory('shareDataService', function () {   

 var formData = {};

    return {
        getData: function () {
            //You could also return specific attribute of the form data instead
            //of the entire data
            return formData;
        },
        setData: function (newFormData) {
            //You could also set specific attribute of the form data instead
            formData = newFormData
        },
        resetData: function () {
            //To be called when the data stored needs to be discarded
            formData = {};
        }
    };
});

从这里向每个控制器注入和请求。

于 2014-11-03T11:59:11.313 回答