【问题标题】:Angular, filter and show one object of array角度,过滤和显示数组的一个对象
【发布时间】:2016-08-06 09:24:08
【问题描述】:

我有一个连接到数组中不同对象的名称列表。我想,每当您单击其中一个名称时,获取值并将它们输出到与该对象相对应的另一个列表中。现在我 console.log 正确的值,但不知道如何输出它:

<a href="#" id="name" ng-click="updateIndex($index);">{{ person.name }}</a>

控制器:

$scope.updateIndex = function(index) {
            console.log($scope.person[index]); //Logs the right values
        }

如何从对象中输出被点击名称的值:

<div class="wrap" ng-repeat="per in person">

        <div class="box">
            {{ person.name }}
        </div>

        <div class="box">
           {{ person.age }}
        </div>

        <div class="box">
           {{ person.town }}
        </div>

        <div class="box">
           {{ person.country }}
        </div>

        <div class="box">
           {{ person.gender }}
        </div>

</div>

谢谢!

【问题讨论】:

  • output the values from the object 是什么意思?

标签: javascript angularjs arrays filter


【解决方案1】:

您可以将变量存储在变量中并使用它:

$scope.updateIndex = function(index) {
     $scope.currentPerson = $scope.person[index] 
}

<div class="wrap" ng-repeat="per in currentPerson">

    <div class="box">
        {{ person.name }}
    </div>

    <div class="box">
       {{ person.age }}
    </div>

    <div class="box">
       {{ person.town }}
    </div>

    <div class="box">
       {{ person.country }}
    </div>

    <div class="box">
       {{ person.gender }}
    </div>
</div>

【讨论】:

  • 完美运行。谢谢!
【解决方案2】:

只需将 $scope.person[index] 分配给 click 函数中的另一个变量,然后在模板中输出即可。

【讨论】:

  • 很高兴听到这个消息。请将其中一个答案标记为正确答案,让大家知道,这个话题已经解决了。谢谢。此外,+1 也是感谢任何帮助您获得正确答案的人的好方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 2017-03-21
相关资源
最近更新 更多