简体   繁体   English

AngularJS limitTo不能在ng-repeat中工作

[英]Angularjs limitTo not working in ng-repeat

I am retrieving a search query from php mysql into my angularjs app. 我正在从php mysql检索搜索查询到我的angularjs应用程序。 The results are in the following format: 结果采用以下格式: 在此处输入图片说明

This object is then displayed in my angularjs app and displayed using ng-repeat. 然后,该对象显示在我的angularjs应用中,并使用ng-repeat显示。 The thing is I would like to limit the results to 10 only. 我想将结果限制为10个。 However for some reason it does not work. 但是由于某种原因,它不起作用。 I did find a possible solution limiTo not working . 我确实找到了一个可能的解决方案limiTo不起作用 Here it states LimitTo does not work if it is an object in an object. 在这里它声明LimitTo如果它是一个对象中的一个对象则不起作用。 If this is the solution how would I convert srchResults to an object in an array in javascript?? 如果这是解决方案,我如何将srchResults转换为JavaScript中数组中的对象? If it is not what would be the issue? 如果不是,那是什么问题?

I'm using Angular 1.3.11. 我正在使用Angular 1.3.11。

My code: 我的代码:

<md-list-item class="md-3-line" ng-repeat="result in srchResults|limitTo:10" >
    <div class="md-list-item-text" ng-click= "getEmployee(result.employee_id)">
        <h3>{{result.last_name}}, {{result.first_name}}</h3>
        <h4>{{result.name}}</h4>
        <p>{{result.fk_cs_type}}, {{result.is_active==1?"Active":"In-Active"}}</p>
        <md-divider ng-if="!$last"></md-divider>
    </div>
</md-list-item>

To answer your query about 回答有关的查询

how would I convert srchResults to an object in an array in javascript 我如何将srchResults转换为JavaScript中数组中的对象

to convert an "object of objects" to an "array of objects" you can do the following: 要将“对象的对象”转换为“对象的数组”,可以执行以下操作:

var obj; //your parent object
var res = [] //will stor result here

for(var i in obj) {
   res.push(obj[i]);
}

after loop ends, you will have res as your desired array 循环结束后,您将获得res作为所需数组

Yet another variant, if you know count fields you can use Array.prototype.slice 还有另一个变体,如果您知道计数字段,则可以使用Array.prototype.slice

var data = {0:{a:1},1:{a:1},2:{a:1},3:{a:1},4:{a:1}}

we know that here 5 fields, so we can do 我们知道这里有5个字段,所以我们可以

data.length = 5;
var arr = Array.prototype.slice.call(data);

 var data = { 0: { a: 1 }, 1: { a: 1 }, 2: { a: 1 }, 3: { a: 1 }, 4: { a: 1 } } data.length = 5; var arr = Array.prototype.slice.call(data); console.log(data, arr); 

Or we can a bit automate this 或者我们可以自动化一点

 var data = { 0: { a: 1 }, 1: { a: 1 }, 2: { a: 1 }, 3: { a: 1 }, 4: { a: 1 } } var keys = Object.keys(data); data.length = 1+ +keys[keys.length-1]; var arr = Array.prototype.slice.call(data); console.log(data, arr); 

Try this code 试试这个代码

<md-list-item class="md-3-line" ng-repeat="(key,value) in srchResults|limitTo:11" >
    <div class="md-list-item-text" ng-click= "getEmployee(value.employee_id)">
        <h3>{{value.last_name}}, {{value.first_name}}</h3>
        <h4>{{value.name}}</h4>
        <p>{{value.fk_cs_type}}, {{value.is_active==1?"Active":"In-Active"}}</p>
        <md-divider ng-if="!$last"></md-divider>
    </div>
</md-list-item>  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM