【问题标题】:How to pass data in angular 1.x如何以角度 1.x 传递数据
【发布时间】:2017-09-10 05:07:54
【问题描述】:

如何将 ng-repeat 中的数据传递给数据属性?

请看下面我的目标。

抱歉格式不佳,这是我第一次在 StackOverflow 上发帖。

<div class="row">
  <div class="col-md-3" ng-repeat="p in controller.prod | filter:{state:'California'}">
    <div class="card">
      <div class="card-block">
        <h3 class="card-title">{{p.productName}}</h3>
        <h6 class="card-subtitle text-muted">${{p.price}}</h6> 
        <img ng-src="{{p.productImage}}" class="img-fluid">
      </div>
      <div class="card-block">
        <div class="card-text">Product information<a 
               href="#" 
               class="card-link productItem btn btn-primary" 
               <!--Below is what i'm trying to accomplish -->
               data-name="{{p.productName}}" 
               data-s="black" 
               data-price="{{p.price}}" 
               data-id="1">Add to Cart</a>
        </div>
        <br />
      </div>
    </div>
  </div>
</div>

【问题讨论】:

  • 什么是数据名数据价格?
  • Data-name, data-price 是我正在做的捕获值,以便我可以将其存储在会话存储中。

标签: html angularjs json


【解决方案1】:

您必须在控制器的 $scope 中分配一个数组,然后在您的 html 模板中将其与 ng-reapeat 绑定,如下所示:

app.controller("myCtrl", function($scope) {
    $scope.records = [
        "Alfreds Futterkiste",
        "Berglunds snabbköp",
        "Centro comercial Moctezuma",
        "Ernst Handel",
    ]
});

你的模板:

<h1 ng-repeat="x in records">{{x}}</h1>

【讨论】:

  • 您好 Hossein,这些值是动态的。我正在尝试获取用户选择的值以将其存储在会话存储中。
猜你喜欢
  • 1970-01-01
  • 2021-01-10
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-24
  • 1970-01-01
  • 2019-08-27
相关资源
最近更新 更多