gpt4 book ai didi

javascript - 使用 POST Angular 将内容添加到不同的路线

转载 作者:行者123 更新时间:2023-11-28 06:53:57 25 4
gpt4 key购买 nike

我是 Angular 的新手,正在尝试使用 $rootScope 来保存从动态生成的选项元素中选择的值,以便我可以在不同的路线上渲染它们。选择选项由 html 中的“ng-model”引用,我想要获取四个不同的 ng-model。这些模型是键值对,我只想要键。

我还包括了我的 HTML,因为我似乎可能需要访问 Controller 内的 ng-model?

    angular.module('FFTrades.tradeanalyzer', [])

.controller('TradesController', function($scope, $rootScope, TradesDataFactory) {

$scope.data = {};
$scope.selectedItem1 = 0;
$scope.selectedItem2 = 0;
//I want to push the selected items here.
$rootScope.data = {};

$scope.getPlayerData = function() {
// getting this from the services.
TradesDataFactory.getPlayerInfo().then(function(playerInfo) {
$scope.data.playerInfo = playerInfo;

}).catch(function(error) {
//console.error(error)
})
}


$scope.calc = function() {
//selectedItem1 is a number. I want its key!
$scope.calculatedValue1 = Number($scope.selectedItem1);
if($scope.selectedItem2){
$scope.calculatedValue1 = Number($scope.selectedItem1) + Number($scope.selectedItem2);

}

}

$scope.calc2 = function() {
$scope.calculatedValue2 = Number($scope.selectedItem3);
if ($scope.selectedItem4) {

$scope.calculatedValue2 = Number($scope.selectedItem3) + Number($scope.selectedItem4);
};

}
for (var key in $scope.data.playerInfo) {
//this is all the players. I just want the ones that have been selected. the problem is $scope.selectedItem4
//for example, is a number, not a key
};
var selectedItems = []
$scope.getPlayerData();

});


<div>Your Team Receives:</div>
<br> Player 1:
<select class="form-control" ng-model="selectedItem1" ng-change="calc()">
<option ng-repeat="(k,v) in data.playerInfo" ng-value="v" >{{k}}
****//{{k}} displays the name of the player only, which is what I want to push to rootScope ****
</option>
</select>
Player 2:
<select class="form-control" ng-model="selectedItem2" ng-change="calc()">
<option ng-repeat="(k,v) in data.playerInfo" ng-value="v">{{k}}</option>
</select>
<!-- show value here -->
<p> Total Value: {{calculatedValue1}} </p>
<br>
<br>
<br>
<div>Their Team Gets:</div>
<br> Player 1:
<select class="form-control" ng-model="selectedItem3" ng-change="calc2()">
<option ng-repeat="(k,v) in data.playerInfo" ng-value="v" >{{k}}</option>
</select>Player 2:
<select class="form-control" ng-model="selectedItem4" ng-change="calc2()">
<option ng-repeat="(k,v) in data.playerInfo" ng-value="v">{{k}}</option>
</select>
<!-- show value here -->
<p> Total Value: {{calculatedValue2}} </p>
<br>

最佳答案

你可以这样使用:

<select class="form-control" ng-model="selectedItem1" ng-change="calc(selectedItem1)">
  $scope.calc = function(model) {
//selectedItem1 is a number. I want its key!
}

关于javascript - 使用 POST Angular 将内容添加到不同的路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32724413/

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