Javascript 尝试在AngularJS$资源调用中使用自定义操作更新值
我一直在努力寻找一种使用angularJS$资源执行put操作的好方法。我有一个映射,它将在我的Web服务中返回一个值数组,我还有POST和PUT端点来执行插入和更新。我让查询工作,以获得值数组并显示在页面上。现在,当我尝试编辑一个值并调用为执行PUT操作而创建的自定义选项“update”时 我的控制器看起来像Javascript 尝试在AngularJS$资源调用中使用自定义操作更新值,javascript,angularjs,rest,angular-resource,Javascript,Angularjs,Rest,Angular Resource,我一直在努力寻找一种使用angularJS$资源执行put操作的好方法。我有一个映射,它将在我的Web服务中返回一个值数组,我还有POST和PUT端点来执行插入和更新。我让查询工作,以获得值数组并显示在页面上。现在,当我尝试编辑一个值并调用为执行PUT操作而创建的自定义选项“update”时 我的控制器看起来像 (function () { 'use strict'; var anErrorOccurred = 'An error has occurred.'; var noRecordsFo
(function () {
'use strict';
var anErrorOccurred = 'An error has occurred.';
var noRecordsFound = 'No records were found.';
angular
.module('customerContactInfo')
.controller('customerContactInfoSearchController', ['$rootScope', '$scope',
'customerContactInfoService', 'utilities',
controllerFunc
]);
function controllerFunc(rootScope, scope,
customerContactInfoService, utilities) {
var self = this;
this.fetchList = function () {
customerContactInfoService.query(buildQueryObject(), function (data) {
if (data.length === 0) {
setPageMsg('infomsg', noRecordsFound);
} else {
setPageData(data, '');
}
}, function (response) {
if (response.status === 404) {
setPageMsg('errormsg', noRecordsFound);
} else {
setPageMsg('errormsg', anErrorOccurred);
}
});
};
function setPageData(data, message) {
scope.customerContactInfoList = data;
scope.error = message;
}
function setPageMsg(type, msg) {
scope[type] = msg;
}
function buildQueryObject() {
console.log(scope.accessToken);
return {
accessToken: scope.accessToken || rootScope.globalAngObj.customerSpaToken,
userId: scope.userId || rootScope.globalAngObj.userId
};
}
function buildQueryObjectForUpdate(customerIdUpdate, customerAddressNewValue) {
return {
accessToken: scope.accessToken || rootScope.globalAngObj.customerSpaToken,
customerId: customerIdUpdate,
customerMessageAddress: customerAddressNewValue
};
}
function initPage() {
scope.customerContactInfoList = undefined;
scope.infomsg = undefined;
scope.errormsg = undefined;
scope.sortType = 'customerLastName'; // set the default sort type
scope.sortReverse = false; // set the default sort order
scope.searchCustomers = ''; // set the default search/filter term
}
function checkChildInit() {
if (scope.$root.initChildApp) {
self.fetchList();
}
}
this.editDirectMsgAddress = function (customerContactInfo) {
var note = customerContactInfoService.query(buildQueryObject());
// Now call `update` to save the changes on the server
note.$promise.then(function () {
customerContactInfoService.$update(buildQueryObject(),
buildQueryObjectForUpdate(customerContactInfo.customerId,
customerContactInfo.customerMessageAddress));
});
};
initPage();
checkChildInit();
}
})();
http://goods.nttf.com/customer-contact-info-ws/data/{userId}
http://goods.nttf.com/customer-contact-info-ws/data/
我的服务看起来像:
(function () {
'use strict';
angular
.module('customerContactInfo')
.factory('customerContactInfoService', ['$resource', 'serviceResolver',
customerContactInfoServiceFactory
]);
function customerContactInfoServiceFactory(resource, serviceResolver) {
return resource(serviceResolver.customerContactInfoWebservice.endpoints.get +
'/:userId/?access_token=:accessToken', {}, {
query: {
method: 'GET',
isArray: true,
timeout: 10000,
withCredentials: true
}
}, {
update: {
method: 'PUT'
}
});
}
})();
我的Html看起来像:
(function () {
'use strict';
angular
.module('customerContactInfo')
.factory('customerContactInfoService', ['$resource', 'serviceResolver',
customerContactInfoServiceFactory
]);
function customerContactInfoServiceFactory(resource, serviceResolver) {
return resource(serviceResolver.customerContactInfoWebservice.endpoints.get +
'/:userId/?access_token=:accessToken', {}, {
query: {
method: 'GET',
isArray: true,
timeout: 10000,
withCredentials: true
}
}, {
update: {
method: 'PUT'
}
});
}
})();
输入客户消息地址以接收零售商品警报。
{{errormsg}}
{{info}}
搜索客户
客户姓氏
客户名
客户ID
客户信息地址
{{item.customerLastName}
{{item.customerFirstName}
{{item.customerId}
我的get url看起来像
(function () {
'use strict';
var anErrorOccurred = 'An error has occurred.';
var noRecordsFound = 'No records were found.';
angular
.module('customerContactInfo')
.controller('customerContactInfoSearchController', ['$rootScope', '$scope',
'customerContactInfoService', 'utilities',
controllerFunc
]);
function controllerFunc(rootScope, scope,
customerContactInfoService, utilities) {
var self = this;
this.fetchList = function () {
customerContactInfoService.query(buildQueryObject(), function (data) {
if (data.length === 0) {
setPageMsg('infomsg', noRecordsFound);
} else {
setPageData(data, '');
}
}, function (response) {
if (response.status === 404) {
setPageMsg('errormsg', noRecordsFound);
} else {
setPageMsg('errormsg', anErrorOccurred);
}
});
};
function setPageData(data, message) {
scope.customerContactInfoList = data;
scope.error = message;
}
function setPageMsg(type, msg) {
scope[type] = msg;
}
function buildQueryObject() {
console.log(scope.accessToken);
return {
accessToken: scope.accessToken || rootScope.globalAngObj.customerSpaToken,
userId: scope.userId || rootScope.globalAngObj.userId
};
}
function buildQueryObjectForUpdate(customerIdUpdate, customerAddressNewValue) {
return {
accessToken: scope.accessToken || rootScope.globalAngObj.customerSpaToken,
customerId: customerIdUpdate,
customerMessageAddress: customerAddressNewValue
};
}
function initPage() {
scope.customerContactInfoList = undefined;
scope.infomsg = undefined;
scope.errormsg = undefined;
scope.sortType = 'customerLastName'; // set the default sort type
scope.sortReverse = false; // set the default sort order
scope.searchCustomers = ''; // set the default search/filter term
}
function checkChildInit() {
if (scope.$root.initChildApp) {
self.fetchList();
}
}
this.editDirectMsgAddress = function (customerContactInfo) {
var note = customerContactInfoService.query(buildQueryObject());
// Now call `update` to save the changes on the server
note.$promise.then(function () {
customerContactInfoService.$update(buildQueryObject(),
buildQueryObjectForUpdate(customerContactInfo.customerId,
customerContactInfo.customerMessageAddress));
});
};
initPage();
checkChildInit();
}
})();
http://goods.nttf.com/customer-contact-info-ws/data/{userId}
http://goods.nttf.com/customer-contact-info-ws/data/
我的post和put url看起来像
(function () {
'use strict';
var anErrorOccurred = 'An error has occurred.';
var noRecordsFound = 'No records were found.';
angular
.module('customerContactInfo')
.controller('customerContactInfoSearchController', ['$rootScope', '$scope',
'customerContactInfoService', 'utilities',
controllerFunc
]);
function controllerFunc(rootScope, scope,
customerContactInfoService, utilities) {
var self = this;
this.fetchList = function () {
customerContactInfoService.query(buildQueryObject(), function (data) {
if (data.length === 0) {
setPageMsg('infomsg', noRecordsFound);
} else {
setPageData(data, '');
}
}, function (response) {
if (response.status === 404) {
setPageMsg('errormsg', noRecordsFound);
} else {
setPageMsg('errormsg', anErrorOccurred);
}
});
};
function setPageData(data, message) {
scope.customerContactInfoList = data;
scope.error = message;
}
function setPageMsg(type, msg) {
scope[type] = msg;
}
function buildQueryObject() {
console.log(scope.accessToken);
return {
accessToken: scope.accessToken || rootScope.globalAngObj.customerSpaToken,
userId: scope.userId || rootScope.globalAngObj.userId
};
}
function buildQueryObjectForUpdate(customerIdUpdate, customerAddressNewValue) {
return {
accessToken: scope.accessToken || rootScope.globalAngObj.customerSpaToken,
customerId: customerIdUpdate,
customerMessageAddress: customerAddressNewValue
};
}
function initPage() {
scope.customerContactInfoList = undefined;
scope.infomsg = undefined;
scope.errormsg = undefined;
scope.sortType = 'customerLastName'; // set the default sort type
scope.sortReverse = false; // set the default sort order
scope.searchCustomers = ''; // set the default search/filter term
}
function checkChildInit() {
if (scope.$root.initChildApp) {
self.fetchList();
}
}
this.editDirectMsgAddress = function (customerContactInfo) {
var note = customerContactInfoService.query(buildQueryObject());
// Now call `update` to save the changes on the server
note.$promise.then(function () {
customerContactInfoService.$update(buildQueryObject(),
buildQueryObjectForUpdate(customerContactInfo.customerId,
customerContactInfo.customerMessageAddress));
});
};
initPage();
checkChildInit();
}
})();
http://goods.nttf.com/customer-contact-info-ws/data/{userId}
http://goods.nttf.com/customer-contact-info-ws/data/
它无法识别资源中的更新函数
appvendor.js:3类型错误:e.$update不是函数
在scripts.js:1
at i(appvendor.js:3)
在appvendor.js:3
n.$digest(appvendor.js:3)
在n.$apply(appvendor.js:3)
在g(appvendor.js:2)
在r(appvendor.js:2)
在XMLHttpRequest.w.onload(appvendor.js:2)“可能未处理的拒绝:{}”
你能再解释一下吗 请记住使用$resource的方法:
var User = $resource('/user/:userId', {userId: '@id'});
User.get({userId: 123}).$promise.then(function(user) {
user.abc = true;
user.$save();
});
可以使用以下参数调用类对象或实例对象上的操作方法:
"class" actions without a body: Resource.action([parameters], [success], [error])
"class" actions with a body: Resource.action([parameters], postData, [success],
[error])
instance actions: instance.$action([parameters], [success], [error])
我知道这不是完整的解决方案,但可以指导您。
$resource
请让我知道我是否可以帮助您进一步了解更多信息我可以使用query而不是get吗