How to update select options after option is selected

时间:2015-07-28 23:55:14

标签: javascript angularjs

I have a select list whose options are based on the selected value. For example, the selected option defaults to 7, and the other four options are always within 2 of this value. So initially b are my options.

If a user selects [5, 6, 7, 8, 9] the select options SHOULD update to 5. Indeed the POST request tied to [3, 4, 5, 6, 7] fires off successfully and upon page refresh the options are ng-change. My issue is that these options do not update instantly upon option selection. Page refresh is currently necessary to see this change.

Below is relevant code. Basically I'm looking to force the [3, 4, 5, 6, 7] array to update instantly upon option selection.

HTML:

maxOptions

Controller:

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

I'm thinking I need to do something within the var vm = this; vm.maxOptions = []; var getplayerInfo = function() { playersService.getPlayerInfo({ playerId: playerId }).$promise.then(function(player) { vm.player = player; for (var i = player.max_points - 2; i < customer.max_points + 3; i++) { vm.maxOptions.push(i); } vm.selectedMax = player.max_points; }); }; var init = function() { getplayerInfo(); }; init(); 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); }); }; resolution in order to update the UI instantly.

1 个答案:

答案 0 :(得分:1)

Your df = df.groupby(['Experiment', 'Step'], as_index=False)['value'].aggregate(np.sum) array is only updated in maxOptions which is called in getplayerInfo. There is no code to update the init when you call maxOptions. Therefore changeMax only gets set on page load but not on change.

You need to do something like this in your changeMax.

maxOptions

Or better yet, abstract the building of the array as a model method like this and call it from both functions:

vm.changeMax = function() {
  playersService.setMaxPoints({
    playerId: playerId,
    max: vm.selectedMax
  }, {}).$promise.then(function(res){
    vm.player.max_points = vm.selectedMax;
    vm.maxOptions = [];
    for (var i = player.max_points - 2; i < customer.max_points + 3; i++) {
      vm.maxOptions.push(i);
    }
    return res.success;
  }, function(res) {
    alert('Couldn\'t update number of points to ' + vm.selectedMax + ':' + res.success);
  });
};
相关问题