角更新和清理厂变量 [英] Angular updating and clearing factory variables
问题描述
我创建一个单页的应用程序中的用户搜索字词,结果被保存在一个变量,一个新的页面被路由,显示的结果。我有这个功能的工作,但是我想,当用户返回到previous页面,最重要的,当用户注销的变量被清除。什么是做到这一点的正确方法?我希望工厂以节省事情,我想某些页面,并清除它们的某些页面,我不想像家或注销。
厂址:
angular.module('firstApp')
.factory(事实,函数(){
变种服务= {};
VAR _information ='默认信息'; service.setInformation =功能(信息){
_information =信息;
} service.getInformation =功能(){
返回_information;
}
退换货服务;
});
控制器:
angular.module('firstApp')
.controller('InformationCtrl',函数($范围,$ HTTP,$位置,事实){
$ scope.message =你好;
$ scope.result = fact.getInformation();
$ scope.sub =功能(形式){
的console.log($ scope.name);
$ scope.submitted = TRUE;
$ http.get('/维基',{
params:一个{
名称:$ scope.name,
}
})。成功(功能(结果){
的console.log(成功!);
$ scope.result =结果;
fact.setInformation(结果);
$ location.path('/ informationdisplay');
});;
}
});
路线
angular.module('firstApp')
的.config(函数($ routeProvider){
$ routeProvider
。当('/信息,{
templateUrl:应用程序/信息/ input.html',
控制器:'InformationCtrl',
验证:真
})
。当('/ informationdisplay',{
templateUrl:应用程序/信息/ results.html',
控制器:'InformationCtrl',
验证:真
});
});
input.html
< DIV CLASS =行>
< DIV CLASS =COL-MD-6 COL-MD-偏移3文本中心>
&所述p为H.; {{结果}}&下; / P>
<窗体类=形式NAME =形式NG提交=子(表)的novalidate>
<输入类型=文本名称=名称占位符=名称级=表格控NG模型=名>
< / BR>
<按钮类=BTN BTN-成功类型=提交级=BTN BTN-INFO>检查< /按钮>
< / DIV>
< / DIV>
results.html
< DIV NG-包括='组件/导航栏/ navbar.html'>< / DIV>
< DIV CLASS =行>
< DIV CLASS =COL-SM-4 COL-SM-偏移4>
&所述; H2>信息结果片剂; / H2>
&所述p为H.; {{结果}}&下; / P>
< / DIV>
< / DIV>
< / DIV>
如果你想让它抹在它们改变航线(其中注销应改变路线也,我假设)的价值,你可以看$ routeChangeStart事件,并有一旦发生任何擦它的价值。你把这个函数在module.run块:
app.run(函数($ rootScope,事实){
$ rootScope。在$($ routeChangeStart功能(事件,接下来,电流){
fact.setInformation(NULL);
});
});
I'm creating a single page app in which a user searches for a term, the result gets saved in a variable, and a new page is routed that displays the result. I have this functionality working, however I want the variable to be cleared when the user returns to the previous page and most importantly when the user logs out. What's the proper way to do this? I want the factory to save things for certain pages that I want, and clear them for certain pages I don't want like "home" or "logout".
Factory:
angular.module('firstApp')
.factory('fact', function () {
var service = {};
var _information = 'Default Info';
service.setInformation = function(info){
_information = info;
}
service.getInformation = function(){
return _information;
}
return service;
});
Controller:
angular.module('firstApp')
.controller('InformationCtrl', function($scope, $http, $location, fact) {
$scope.message = 'Hello';
$scope.result = fact.getInformation();
$scope.sub = function(form) {
console.log($scope.name);
$scope.submitted = true;
$http.get('/wiki', {
params: {
name: $scope.name,
}
}).success(function(result) {
console.log("success!");
$scope.result = result;
fact.setInformation(result);
$location.path('/informationdisplay');
});;
}
});
Routes
angular.module('firstApp')
.config(function ($routeProvider) {
$routeProvider
.when('/information', {
templateUrl: 'app/information/input.html',
controller: 'InformationCtrl',
authenticate : true
})
.when('/informationdisplay', {
templateUrl: 'app/information/results.html',
controller: 'InformationCtrl',
authenticate : true
});
});
input.html
<div class="row">
<div class="col-md-6 col-md-offset-3 text-center">
<p>{{result}}</p>
<form class="form" name="form" ng-submit="sub(form)" novalidate>
<input type="text" name="name" placeholder="Name" class="form-control" ng-model="name">
</br>
<button class="btn btn-success" type="submit" class="btn btn-info">Check</button>
</div>
</div>
results.html
<div ng-include="'components/navbar/navbar.html'"></div>
<div class="row">
<div class="col-sm-4 col-sm-offset-4">
<h2>Information Results</h2>
<p>{{result}}</p>
</div>
</div>
</div>
If you want it to wipe the value when they change routes (which logout should change routes also, I assume), you can watch the $routeChangeStart event and have it wipe the value whenever it occurs. You put that function in the module.run block:
app.run(function ($rootScope, fact) {
$rootScope.$on("$routeChangeStart",function(event, next, current){
fact.setInformation(null);
});
});
这篇关于角更新和清理厂变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!