Javascript 重置范围变量-Angular.js
我想确保isSearchVisible范围变量在每次页面更改时总是以false开头。我应该如何实施它 app.controller'MainController',函数$rootScope{ $rootScope.isSearchVisible=false; }; app.controller'ProfileController',函数$scope{ $scope.isSearchVisible=true; }; app.controller'AboutUsController',函数$scope{ }; 在每个页面中,根作用域变量isSearchVisible因MainController而为false 当您进入profile页面ProfileController时,本地范围变量将变为true,这很好 但是当您离开此页面时,根范围变量也更改为true。$rootScope变量和$scope变量之间没有分离Javascript 重置范围变量-Angular.js,javascript,angularjs,angularjs-scope,angularjs-rootscope,Javascript,Angularjs,Angularjs Scope,Angularjs Rootscope,我想确保isSearchVisible范围变量在每次页面更改时总是以false开头。我应该如何实施它 app.controller'MainController',函数$rootScope{ $rootScope.isSearchVisible=false; }; app.controller'ProfileController',函数$scope{ $scope.isSearchVisible=true; }; app.controller'AboutUsController',函数$scop
除非控制器直接更改了isSearchVisible,否则在每次页面更改时如何将其重置为false?将事件侦听器添加到根作用域,然后执行所需操作 $rootScope.$on'$routeChangeStart',functionnext,当前{ ... 如果要在其中显示搜索框,请选中url url make$rootScope.showSearchBlock=true 否则将其设为false。并删除 $rootScope.showSearchBlock 把这个放在你的主控制器上 ...
}; 您必须使用服务共享数据:
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 = {};
}
};
});
从此处插入并请求每个控制器。AboutsController是否嵌套在ProfileController下?请尝试在更改路由的配置部分中进行尝试。由于$scope是以Angular模式继承的,因此AboutsController会获取ProfileController的$scope,这将重新定义showSearchBlock字段。@MatanYedayev很高兴看到您得到了答案。继续工作