gpt4 book ai didi

javascript - 在 Angular 中,在选定的选项上为其他选项设置默认值

转载 作者:行者123 更新时间:2023-11-28 19:40:05 24 4
gpt4 key购买 nike

问题:如何将选定尺寸作为索引来映射并显示正确的pricetsin eq到尺寸索引

<小时/>

注意:

  • 我正在尝试在尺寸、价格、tsin 之间建立关系

      "Black": {
    "sizes": "X Small, Small",
    "prices": '$22.24, $24.94',
    "tsin": "111002, 111003"
    },

例如如果选择黑色并且选择的尺寸为索引 [0](或 - X Small),那么在 ng-change 上,价格将更新为 22.24 美元,tsin 为 111002

<小时/>

更新:我已经更新了 fiddle 以使其更接近,但原型(prototype)继承不存在。我只是想说明一下想要的效果

<强> UPDATED JSFiddle

更新了 2 次Callebe 给了我一个解决方案,让我更接近,但仍然不理想或无法正常工作

JSFiddle

我有一个简单的 Controller ,其中有一些来自 $http: 的虚假数据:

var app = angular.module("app", []);

app.controller('Ctrl', function ($scope, $filter, $http) {
//lowest price is coming in from the init api request on production
var lowestPrice = '$21.24';
// exposes currentPrice to the view
$scope.currentPrice = lowestPrice;

// function to change the currentPrice to the selected Products price
$scope.getCurrentPrice = function(idx) {
$scope.currentPrice = idx;
}
//fake data that simulates a piece of the json object response.
$scope.productData = {
"colors_and_sizes": {
"data": {
"Black": {
"prices": '$21.24, $29.94',
"swatch_image": 'http://lorempixel.com/50/50',
"sizes": "X Small, Small",
"prices": '$22.24, $24.94',
"tsin": "111002, 111003"
},
"Blue": {

"swatch_image": 'http://lorempixel.com/50/51',
"sizes": "X Small, Small",
"prices": '$22.24, $24.94',
"tsin": "112005, 112007"
}
}
}
};

});

注意:

  • 我知道我知道,对于我想要做的事情来说,数据输入得非常糟糕。

尺寸prices和tsin是字符串。但是,当我到达 View 时,我将它们分开:

<form ng-app="app" ng-controller="Ctrl" ng-init="item = this">
<div class="color-pick"
ng-repeat="(key, val) in productData.colors_and_sizes.data track by $index">
<input
type="radio"
name="colors"
hidden="true"
ng-model="item.myColor"
ng-value="key" />

<img
ng-click="item.myColor = key"
ng-src="{{val.swatch_image}}"
alt="">
{{key}}

<div class="size-pick" ng-show="item.myColor==key">
<select
ng-model="item.mySize"

<!--split the sizes into an array call it choice -->
<!--idx as choice could be idx to get key but then breaks the value cannot do (idx, choice) as choice for (idx, choice) in val.sizes.split(',') -->
ng-options="choice as choice for (idx, choice) in val.sizes.split(',')">
<option value="">Please select a size</option>

ng-change="setCurrentPrice(val.sizes.split(',')[idx])"
</select>
</div>

</div>
</form>

最后我希望在 View 上显示 4 个值:

mySize: {{ item.mySize}}
myColor: {{item.myColor}}
myPrice: {{item.currentPrice}}
myTsin: {{item.myTsin}}

请再次查看我的(上面的旧 fiddle 更新) JSfiddle

最佳答案

很简单,看看这个

<强> Working Demo

html

<select ng-model="item.mySize" ng-change="setCurrentPrice(val.sizes.split(','),val.prices.split(','),val.tsin.split(','))" ng-options="choice as choice for (idx, choice) in val.sizes.split(',')">
<option value="">Please select a size</option>
</select>

脚本

$scope.setCurrentPrice = function(sizes, prices, tsin) {
var index = sizes.indexOf($scope.item.mySize);
$scope.item.myPrice = prices[index];
$scope.item.myTsin = tsin[index];
$scope.item.currentPrice = prices[index];
$scope.item.mySize = sizes[index];
};

关于javascript - 在 Angular 中,在选定的选项上为其他选项设置默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25256187/

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