0

I'm new in Angularjs and I'm developing a kind of shopping cart I have and array:

 $scope.products = [
{id: '141', name: 'Apple', qty:0}, 
{id: '223', name: 'Orange', qty:0},
{id: '398', name: 'Banana', qty:0}];

By design limitation I need to do something like

<input type="text" ng-model="products[productID].qty"/>

(I can't use array element index I know it works.)

I've tried also with

ng-model="(p in products | filter: {id:'223'}).qty"

But I couldn't make it work

Please Could you help me ??

I will explain my shopping cart Let imagine that I need a table in the columns the sizes:s,m,l, xl in the rows models, but there are models that are manufactured in all sizes so I will have an input in every cell, but there are some that are only in l and xl and other in s and m So I will have a table with some empty cells. I know I can have a table that works as metadata where I can define columns, rows and the mapping and then develop a directive. My idea is design the table in HTML and assign to each cell the productID

4

1 回答 1

1

基本上,您将不得不从数组中选择 id,因此在您的指令/控制器中编写如下内容:

$scope.getProduct = function(id) {
  var product;
  angular.forEach($scope.products, function(p) {
    if(p.id === id) {
      product = p;
    }
  });
  return product;
};

然后:

$scope.selectedProduct = $scope.getProduct('223');

然后在你的html中:

<input ng-model="selectedProduct.qty"/>

编辑:我不知道这是否是你在说的,但这里有。(你知道ngRepeat吗?):

<table>
  <thead>... stuff</thead>
  <tbody>
    <tr ng-repeat="product in products">
      <td ng-bind="product.id"></td>
      <!-- bind other stuff here -->
      <td>
        <input ng-model="product.qty"/>
      </td>
    </tr>
  </tbody>
</table>
于 2014-05-22T20:01:43.200 回答