我使用带有变量的角度服务,我的app.js看起来像......,
app.service('sessionService', function() {
var users = [
name: ' ',
age: ' ',
gender: M,
marStatus: single
];
});
我有一个注销选项,如何将我的变量用户重置为默认选项?
答案 0 :(得分:0)
您需要使用http调用服务器操作来创建服务。响应将始终以正确的方式设置数据。
var app = angular.module('myApp', []);
app.service('getUserDetailsService', ['$http', function($http) {
this.getDetails = function() {
var details = {name:'',age:'',gender:'M'}
$http.get('Your action url')
.success(function(data) {
details.name = data.name;
details.age = data.age;
details.gender = data.gender;
})
.error(function() {
//handle error
});
return details;
};
}]);
//Controller
app.controller("userController",function($scope,getUserDetailsService){
$scope.getDetails = function() {
getUserDetailsService.getDetails().then(function(detail) {
$scope.details = detail;
}, function(error) {
});
}
$scope.getDetails() //Call whenever you want
});
小提琴链接 - https://jsfiddle.net/ftzyyt0L/3/