重置范围变量Angular.js
Reset a scope variable - Angular.js
我想确保每次页面更改时,isSearchVisible
作用域变量总是从false
开始。我应该如何实施它?
app.controller('MainController', function($rootScope) {
$rootScope.isSearchVisible = false;
});
app.controller('ProfileController', function($scope) {
$scope.isSearchVisible = true;
});
app.controller('AboutUsController', function($scope) {
});
在每个页面中,根作用域变量isSearchVisible
是false
,因为MainController
。
当您进入配置文件页面(ProfileController
)时,本地作用域变量将变为true
,这很好。
但是当您离开此页面时,根作用域变量也更改为true
。$rootScope
变量和$scope
变量之间没有分离。
每次页面更改时,我应该如何将isSearchVisible
重置为false
,除非控制器直接更改它?
将事件侦听器添加到根作用域,然后执行您想要的
$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
...
});
您必须使用服务来共享数据:
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 = {};
}
};
});
从这里注入并请求每个控制器。
相关文章:
- Angular JS IE9 Hashbang url rewriting
- 无法在数据endVal中设置值=“”;{{ucount}}”;使用Angular JS的CountUp
- angular.js没有'无法在PhoneGap中处理视图标记
- Angular js-返回一个包含类似
- 如何使用 Angular JS 将数据保存在数据库中
- 将Angular js与taglib结合使用
- Angular JS Filter-通过3个复选框进行筛选
- vaadin:使用自定义布局集成angular js
- angular js密码强度显示问题
- 使用angular.js问题的JavaSpringREST服务
- 将复杂对象从angular js传递到web api,它总是返回404
- entre上的angular js阻止提交
- 根据Angular.JS上一次的内容禁用选择
- 如何在angular js中使用指令.不适用于我的情况,为什么
- 预期响应包含一个对象,但在angular js中得到一个数组错误
- angular.js:13424错误:[ng:areq]参数'fn'不是函数,getObject
- 使用Angular js-量角器e2e测试上传文件
- Angular.js延迟控制器初始化
- 如何在angular.js中动态应用自定义过滤器
- 谷歌地图/Angular JS:地图更新并不总是反映数据,并且可以't删除标记