我正在尝试更新然后获取 AngularJs 工厂的更新值,我在一个范围内设置值并尝试在另一个范围内获取它,我正在设置一个工厂来处理我的应用程序中的弹出消息,而不是重复我自己我只想要一个工厂来处理这些消息。
myApp.factory('msg',function ($modal) {
factory = {};
factory.text = '';
factory.modal;
factory.response;
factory.window = function () {
this.modal = $modal.open({
templateUrl:'partials/message/confirm.html',
controller:'cms'
});
}
factory.setText = function (text) {
this.text = text;
}
factory.getResponse = function () {
return this.response;
}
factory.confirm = function () {
this.modal.close();
this.response = true;
}
factory.cancel = function () {
this.modal.close();
this.response = false;
}
factory.confirmSave = function () {
this.text = 'save';
this.window();
}
factory.confirmUpdate = function () {
this.text = 'update';
this.window();
}
factory.confirmDelete = function () {
this.text = 'delete';
this.window();
}
return factory;
});
用户控制器
myApp.controller('users',function ($scope,$location,$http,msg) {
$http.get('api/v1/users')
.success(function (data,status) {
$scope.user = data;
})
.error(function (data,status) {
$location.path('/login');
});
$scope.showWrite = function () {
$scope.write = true;
}
$scope.closeWrite = function () {
$scope.write = false;
$scope.newUser = '';
}
$scope.save = function () {
$http.post('api/v1/users/store',$scope.newUser)
.success(function (data,status) {
$scope.user.unshift({
first_name: $scope.newUser.first_name,
last_name: $scope.newUser.last_name,
email: $scope.newUser.email,
role: $scope.newUser.role
});
$scope.write = false;
$scope.newUser = '';
})
.error(function (data,status) {
alert('failed');
});
}
$scope.confirmDelete = function (index,id) {
msg.confirmDelete();
if(msg.getResponse() === true){
$http.get('api/v1/users/destroy/'+id)
.success(function (data,status) {
$scope.user.splice(index,1);
})
.error(function (data,status) {
alert('failed');
});
};
console.log(msg.getResponse());
}
$scope.showUserInfo = function () {
}
});