简体   繁体   English

如何根据用户从下拉列表中的选择访问数组元素的其他字段,以及如何使用 javascript 删除重复项

[英]How to access other fields of array elements based on user selection from dropdown list and also how to remove the duplicates using javascript

Below is the part of code in main html file以下是主 html 文件中的部分代码

<select ng-model="UserSelected">
  <option value="" selected disabled>--Product--</option>
  <option ng-repeat="Product in proservice">{{Product.type}}</option>
</select>{{UserSelected}}

Below is the AngularJS code with array here there is array of products with type such as robot,mobile,tv etc. I want to fetch array row according to the type selected by the user.下面是带有数组的 AngularJS 代码,这里有一系列类型的产品,例如机器人、移动设备、电视等。我想根据用户选择的类型获取数组行。

app.service('myService', function () {
  this.setProduct = function () {
    this.Products = [
        {id: 1, images: 'D:/angular/shop/src/home/robot3.jpg', Names: "ASIMO ", Price: "$1000", detail: "hello",type:"robot"},
        {id: 2,images: "D:/angular/shop/src/home/robot2.jfif", Names: "Bionic Hand", Price: "$2000", detail: "hi",type:"robot" },

        {id: 6, images: "D:/angular/shop/src/home/mobile3.png", Names: "Xiaomi", Price: "$3000", detail: "heyyy",type:"mobile" },
        {id: 7, images: 'D:/angular/shop/src/home/mobile4.jpg', Names: "oppo", Price: "$1000", detail: "hello",type:"mobile" },
        {id: 8, images: "D:/angular/shop/src/home/laptop1.jpg", Names: "Dell", Price: "$2000", detail: "hi",type:"laptop" },
        {id: 10, images: "D:/angular/shop/src/home/laptop2.jpg", Names: "Lenovo", Price: "$3000", detail: "heyyy",type:"laptop" },
        {id: 11, images: 'D:/angular/shop/src/home/laptop3.jpg', Names: "IBM", Price: "$1000", detail: "hello",type:"laptop" }] 
    return this.Products;
  };
  this.getProduct= function(){
    return this.Products;
  }
});

app.controller('HomeController', ['$scope', '$window','myService', function ($scope, $window, myService) {
  $scope.proservice = myService.setProduct();
}]);

If you want to access user selection, then you need to add value attr on your <option> tag.如果要访问用户选择,则需要在<option>标签上添加value attr。 After that, you need to add listener like ng-change="onProductSelect()" near ng-model="UserSelected" and after that you will be able to track changes on <select> element.之后,您需要在ng-model="UserSelected"附近添加ng-change="onProductSelect()" ng-model="UserSelected" ,之后您将能够跟踪<select>元素上的更改。

Example:例子:

<select ng-model="selectedProduct" ng-change="onProductSelect()">
  <option value="" selected disabled>--Product--</option>
  <option ng-repeat="product in products" value="{{ product.type }}">{{ product.type }}</option>
</select>

app.controller('HomeController', ['$scope', '$window','myService', function ($scope, $window, myService) {
  $scope.selectedProduct = '';
  $scope.products = myService.getProducts();

  $scope.onProductSelect = () => {
    $scope.products = $scope.products.filter(product => product.type === $scope.selectedProduct);
  };
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM