html - 如何在 arraylist 中获取 arraylist where index = x

标签 html angularjs

你好,这是我第一次在堆栈溢出问题上提问,请原谅你在我的问题中看到的错误。

我是网络编程和 Angular 方面的新手。 我创建了一个处方 list ,处方里面有药品 list

.controller('prescriptionTable',function($scope) {
var Prescription = [
    {id:1,secID:1,docID:1,medicine:[
        {name:"lozartan",quantity:2},
        {name:"biogesic",quantity:3},
        {name:"paracetamol",quantity:1},]
    },
    {id:2,secID:1,docID:1,medicine:[
        {name:"Lumiracoxib",quantity:2},
        {name:"Cloxacillin",quantity:3},
        {name:"Allopurinol",quantity:4}]
    },
];

$scope.Prescription = Prescription;
$scope.indexNum =0;

然后我使用 ng-repeat 这样我就可以获得药品 list

<div ng-controller="prescriptionTable">
    <ul>
        <li ng-repeat="px in Prescription">
            {{px.id}}
            <ul>
                <li ng-repeat="med in px.medicine">
                    Name: {{med.name}} , Quantity: {{med.quantity}}
                </li>
            </ul>
        </li>
    </ul>
</div>

如何使用 $scope.indexNum 过滤处方以仅显示特定处方的药物列表?

感谢和欢呼。

最佳答案

如果您只想允许一个索引,您可以将其指定为

$scope.indexNum = 0;

在你的 html 中

<li ng-repeat="px in Prescription track by $index" ng-if="$index == indexNum">

你甚至可以硬编码一个值 0

<li ng-repeat="px in Prescription track by $index" ng-if="$index == 0">

如果你只想显示一个项目,你应该完全避免ng-repeat

<div ng-controller="prescriptionTable">
    <ul>
        <li>
            {{Prescription[indexNum].id}}
            <ul>
                <li ng-repeat="med in Prescription[indexNum].medicine">
                Name: {{med.name}} , Quantity: {{med.quantity}}
                </li>
            </ul>
        </li>
    </ul>
</div>

你可以有多个索引

<div ng-controller="prescriptionTable">
    <ul>
        <li ng-repeat="px in Prescription track by $index" ng-if="allowedIndexes.indexOf($index) > -1">
            {{px.id}}
            <ul>
                <li ng-repeat="med in px.medicine">
                    Name: {{med.name}} , Quantity: {{med.quantity}}
                </li>
            </ul>
        </li>
    </ul>
</div>

在你的js中你会有一个数组

$scope.allowedIndexes = [0,2,5,7,9];

你可以使用 Angular 过滤器

<div ng-controller="prescriptionTable">
    <ul>
        <li ng-repeat="px in Prescription|filter:{id:indexNum}">
            {{px.id}}
            <ul>
                <li ng-repeat="med in px.medicine">
                    Name: {{med.name}} , Quantity: {{med.quantity}}
                </li>
            </ul>
        </li>
    </ul>
</div>

这将过滤所有id包含indexNum值的对象,即如果indexNum为1,则将有对象id 等于 1, 10, 11, 12, 21... 显示。

关于html - 如何在 arraylist 中获取 arraylist where index = x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35007827/

相关文章:

html - 使用 Bootstrap Grid 对齐内容

html - 防止 <a> 继承 text-align : justified?

javascript - 如何通过单击按钮调用具有不同选择选项的不同功能

html - Gmail - CSS - 列计数不起作用

html - 如何使 "Fixed"元素可滚动

javascript - AngularJS:RangeError:Date.toISOString (<anonymous>) 的无效时间值

javascript - 我如何使用 $q all angular.js 处理多个 http 请求

javascript - 如何在javascript中显示小数点后两位数

exception-handling - AngularJS中应用程序异常处理的推荐做法

Angularjs 初始化服务