Javascript 如何在正确的道路上书写factorys AngularJS
我找到了angularjs的样式指南: 我现在想用这种方式编写代码: 这是我的工作工厂:Javascript 如何在正确的道路上书写factorys AngularJS,javascript,angularjs,Javascript,Angularjs,我找到了angularjs的样式指南: 我现在想用这种方式编写代码: 这是我的工作工厂: .factory('Noty',function($http){ return{ update:function(NotificationData){ return $http ({ method: "PUT", url
.factory('Noty',function($http){
return{
update:function(NotificationData){
return $http ({
method: "PUT",
url : "/api/notification/" + NotificationData.task,
data : NotificationData
});
}
};
});
我怎样才能把它改写成上面文档中的样子
/* recommended */
function dataService() {
var someValue = '';
var service = {
save: save,
someValue: someValue,
validate: validate
};
return service;
////////////
function save() {
/* */
};
function validate() {
/* */
};
}
这非常简单,因为您已经在使用一个具有以下功能的工厂:
你可以这样写:
(function () {
'use strict';
var factoryNotyModule = angular.module('yourApp.factory.Noty', []);
factoryNotyModule.factory('Noty', Noty);
Noty.$inject = ['$http'];
function Noty($http) {
function updateData (NotificationData) {
return $http({
method: 'PUT',
url: '/api/notification/' + NotificationData.task,
data: NotificationData
});
}
return {
updateData: updateData
};
}
})();
这样,您可以在将其注入某个位置后使用它:
Noty.updateData(data);
Noty.updateData(data);