【问题标题】:AngularJS assign values to items in ng-repeat array using ng-model and retrieve those valuesAngularJS 使用 ng-model 为 ng-repeat 数组中的项目分配值并检索这些值
【发布时间】:2015-04-29 16:55:58
【问题描述】:

这比预期的要困难得多。

我有两个数组:一个用于产品,一个用于选项。每个产品都应该有自己的一系列选项。

The problem starts when I try to give each product its own options array with its own scope, meaning when one option is selected it only pertains to that product's $scope and not any other.

有人建议我将 [$index] 添加到数组中,但这似乎不起作用。我无法检索此数组中的特定值。

options 数组有以下 id:(name,price,active)。在我尝试通过将活动布尔值设置为 true 并过滤这些结果来尝试执行此操作之前......但无论哪种方式都可以,简而言之,我需要检索所选选项的名称和价格值......

这不是最容易解释的,所以请耐心等待...感谢您的关注。

选项视图 html

<md-card ng-repeat="item in items.results | filter:true">
     <img ng-src="{{item.img}}" 
          class="md-card-image" 
          alt="">
     <md-card-content class="content">
          <h2 class="md-title">{{ item.name }}</h2>
          <h4>{{ item.price | currency }}</h4>
          <md-list>
              <p class="md-subhead">Choose Your Flavor</p>
              <md-divider></md-divider>
              <md-list-item ng-repeat="option in options.results" 
                            layout="row">
                  <p> {{ option.name }} </p>
                  <span flex></span>
                  <p> {{ option.price | currency}} </p>
                  <md-checkbox aria-label="option.active"
                               class="md-accent" 
                               ng-model="item.flavor[$index]">
                  </md-checkbox>
              </md-list-item>
          </md-list>
       </md-card-content>
       <md-action-bar layout="row" 
                      layout-align="end center">
           <md-button class="md-fab md-accent fab" 
                      aria-label="Remove From Cart" 
                      ng-click="remove(item)"                               
                      ng-class="{active:item.active}">
               <md-icon md-svg-src="img/icons/remove.svg"></md-icon>
           </md-button>
       </md-action-bar>
 </md-card>

这是我尝试使用这些值的顺序 html..

<md-card>
    <md-card-content>
        <md-list>
            <span class="md-subhead">Review Order</span>
            <md-divider></md-divider>
            <md-list-item ng-repeat="item in items.results | filter:true" 
                          layout="row">
                <span>{{ item.name }}</span>
                <span flex></span>
                <span>{{ item.price | currency}}</span>
                <span ng-repeat="flavor in item.flavor | filter:true">
                    {{flavor}}
                </span>          
            </md-list-item>
            <md-divider></md-divider>
            <md-list-item layout="row">
                <span>Order Total :</span>
                <span flex></span>
                <span>{{ total(items.results) | currency}}</span>
            </md-list-item>
        </md-list>   
    </md-card-content>
</md-card>

【问题讨论】:

    标签: arrays angularjs angularjs-ng-repeat angular-ngmodel


    【解决方案1】:

    您可以在控制器中创建以下函数,而不是尝试使用 ng-model 指令

    $scope.addOption = function(item, opt) {
        var index = item.flavors.indexOf(opt);
        if(index > -1) {
            item.flavors.splice(index, 1);
        }
        else
            item.flavors.push(opt);
    }
    
    $scope.checkOption = function(item, opt) {
        return item.flavors.indexOf(option) > -1
    }   
    

    并将您的 html 更改为

    <md-checkbox aria-label="option.active"
               class="md-accent" 
                ng-checked="checkOption(item, option)"
               ng-click="addOption(item, option)">
    </md-checkbox>
    

    然后您可以按如下方式访问风味

    <div ng-repeat="flavor in item.flavors">[{{flavor.name}} : {{flavor.price}}] </div> 
    

    你可以测试一下这个想法here

    【讨论】:

    • 这是一个很好的解决方案.. 和一个简单的功能添加到我的工厂.. 但是我将如何检索 {{ item.flavours[option.name] }} 中的值?
    • 原始代码中存在一些错误,现在已修复并添加了访问方式。
    • 像魅力一样工作...你知道为什么复选框在未选中之前不会变为活动状态...我在您的代码中将默认值更改为 false ..但它们只有在它们之后才会突出显示未选中并且活动是错误的...奇怪...如果您对此有任何见解会很棒,但感谢您提供一个好的解决方案
    • 我也在努力解决这个问题
    猜你喜欢
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多