gpt4 book ai didi

javascript - 如何根据我的需求在 angularjs 中显示数据?

转载 作者:行者123 更新时间:2023-11-28 18:43:42 26 4
gpt4 key购买 nike

在此示例中,基于带有复选框的移动品牌选择移动型号,一切正常。基于用户品牌选择的移动型号即将推出。

对于 htc 品牌,当用户点击 htc 模型时,我有带有图像的模型应该显示图像而不是显示

Htc One X9

欲望820

欲望810S

它应该显示带有复选框的图像,但在提交时它应该传递模型名称。任何一位帮助表示赞赏

var myApp = angular.module('myApp', []);

myApp.controller('MyCtrl', function($scope, $http) {
$scope.selectedBrands = [];

$scope.selectBrand = function(selectedphone) {
// If we deselect the brand
if ($scope.selectedBrands.indexOf(selectedphone.brandname) === -1) {
// Deselect all phones of that brand
angular.forEach($scope.phones, function(phone) {
if (phone.brandname === selectedphone.brandname) {
phone.selected = false;
}
});
}
}

$scope.checkSelectedphones = function() {
var modelNames = [];
var jsonArr = [];
var subModelArr = [];
var aletrMsg = '';
angular.forEach($scope.phones, function(phone) {
if (phone.selected) {
modelNames.push(phone);
var found = jsonArr.some(function(el) {
if (el.model === phone.brandname) {
el.subModel.push(phone.modelname);
return true;
}
});

if (!found) {
subModelArr.push(phone.modelname);
jsonArr.push({
model: phone.brandname,
brand: 'Nokia',
subModel: subModelArr,
city: 'Noida',

});
subModelArr = [];
}

}

phone.selected = false;
});
console.log(modelNames.length);
if (modelNames.length == 0) {
alert(modelNames.length ? aletrMsg : 'No phones selected!');
} else {
console.log(jsonArr);
}

$scope.selectedBrands = [];
}

$scope.phones = [{
id: "986745",
brandname: "Nokia",
modelname: "Lumia 735 TS"
}, {
id: "896785",
brandname: "Nokia",
modelname: "Nokia Asha 230"
}, {
id: "546785",
brandname: "Nokia",
modelname: "Lumia 510"
}, {
id: "144745",
brandname: "Samsung",
modelname: "Galaxy Trend 840"
}, {
id: "986980",
brandname: "Samsung",
modelname: "Galaxy A5"
}, {
id: "586980",
brandname: "Samsung",
modelname: "Galaxy Note 4 Duos"
}, {
id: "986980",
brandname: "Samsung",
modelname: "Galaxy A5"
}, {
id: "586980",
brandname: "Samsung",
modelname: "Galaxy Note Duos"
}, {
id: "232980",
brandname: "Htc",
modelname: "Htc One X9",
image:"http://cdn.bgr.com/2015/03/bgr-htc-one-m9-1.jpg",
}, {
id: "456798",
brandname: "Htc",
modelname: "Desire M9",
image:"https://vtcdn.com/sites/default/files/images/2014/6/10/img-1402379949-1.jpg",
}, {
id: "656798",
brandname: "Htc",
modelname: "Desire 810S",
image:"https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQINOLh4PEebd7H8F5MM9SGdC14oQAH91I4XqHJZL3LlUg1PKoV",
}];

});

myApp.filter('unique', function() {
return function(collection, keyname) {
var output = [],
keys = [];

angular.forEach(collection, function(item) {
var key = item[keyname];
if (keys.indexOf(key) === -1) {
keys.push(key);
output.push(item);
}
});

return output;
};
});
<div ng-controller="MyCtrl">
<button ng-click="checkSelectedphones()">
Check selected phones
</button>

<div ng-repeat="phone in phones | unique:'brandname'">
<label>
<input type="checkbox" ng-true-value="'{{phone.brandname}}'" ng-false-value="''" ng-model="selectedBrands[$index]" ng-change="selectBrand(phone)"> {{phone.brandname}}
</label>
</div>

<br>

<div ng-repeat="brand in selectedBrands track by $index" ng-if="brand">
{{brand}}
<div ng-repeat="phone in phones" ng-if="phone.brandname === brand">
<label>
<input type="checkbox" ng-model="phone.selected"> {{phone.modelname}}

</label>
</div>
</div>
</div>

demo

最佳答案

我根据你的代码制作了一个 fiddle 。链接如下:https://jsfiddle.net/0Lggc5pn/

只有 HTC 手机有图像。所以我所做的就是添加以下代码:

<img ng-if="phone.image" src="{{phone.image}}" width="20" height="20">
<span ng-if="!phone.image">{{phone.modelname}}</span>

因此,如果有可用图像,则会显示该图像。否则将显示型号名称。

我还删除了名为 selectBrand() 的方法,这似乎没有必要。

关于javascript - 如何根据我的需求在 angularjs 中显示数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35715456/

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