gpt4 book ai didi

javascript - 使用 ng-change POST 新值

转载 作者:行者123 更新时间:2023-12-03 09:25:36 24 4
gpt4 key购买 nike

我尝试在 Angular 应用程序中选择一个选项后触发 POST 请求。下面是我当前的代码。

HTML:

<select ng-options="option for option in linked.maxOptions" ng-model="linked.selectedMax" ng-change="linked.changeMax()"></select>

Controller :

var playerId = $routeParams.playerId;

vm.changeMax = function() {
playersService.setMaxPoints({
playerId: playerId,
max: vm.selectedMax
}).$promise.then(function(res){
return res.success;
}, function(res) {
alert('Couldn\'t update number of points to ' + vm.selectedMax + ':' + res.success);
});
};

服务:

angular.module('gameApp')
.factory('playersService', ['$resource',
function($resource) {
var base = '/api/players/:playerId/';
return $resource(base, {}, {
setMaxPoints: {method: 'POST', url: base + 'maxPoints/' + ':max'}
});
}]);

问题是,当我的服务方法尝试访问此端点时,由于某种原因,我的参数没有被传递到我的服务方法:

http://localhost:3010/api/players/maxPoints

最佳答案

playerId 从哪里来?它不会被声明,也不会作为参数传递给您的 changeMax 函数。

这是我声明资源的方式。语法比您的更简单,因此不太容易出错:

angular.module('myApp')
.factory('ActivityData', function($resource) {
return $resource('/service/data/:userEmail/:dataType', {dataType: 'all'},
{
getTotals: {method:'GET', cache: true, params: { dataType: 'total'}}
}

});

关于javascript - 使用 ng-change POST 新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31688639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com