【发布时间】:2016-11-01 22:37:44
【问题描述】:
如何获取输入文本的值并与带有数组的对象的值进行比较?
当我输入 cmets 数组的属性名称时,必须按输入的文本排序。
例如按作者键入作者顺序。 我对此进行了搜索,阅读了文档,但没有找到解决方案。 我可以按顺序传递一个函数吗?我尝试了,但没有成功。
我发现太多示例只传递一个值或不传递输入文本,我想传递 3 个选项来订购,如果作者、评级或日期,但如果有人在输入文本中键入此词。
为什么不能这样工作:
Sort by: <input type="text" ng-model="**sortBy**">
<div ng-repeat="dishcomment in dishCtrl.dish.comments|filter:**sortBy**">
var dish={
name:'Uthapizza',
image: 'images/uthapizza.png',
category: 'mains',
label:'Hot',
price:'4.99',
description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.',
comments: [
{
rating:5,
comment:"Imagine all the eatables, living in conFusion!",
author:"John Lemon",
date:"2012-10-16T17:57:28.556094Z"
},
{
rating:4,
comment:"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
author:"Paul McVites",
date:"2014-09-05T17:57:28.556094Z"
},
{
rating:3,
comment:"Eat it, just eat it!",
author:"Michael Jaikishan",
date:"2015-02-13T17:57:28.556094Z"
},
{
rating:4,
comment:"Ultimate, Reaching for the stars!",
author:"Ringo Starry",
date:"2013-12-02T17:57:28.556094Z"
},
{
rating:2,
comment:"It's your birthday, we're gonna party!",
author:"25 Cent",
date:"2011-12-02T17:57:28.556094Z"
}
]
};
this.dish = dish;
Sort by: <input type="text" ng-model="searchBox">
<div ng-repeat="dishcomment in dishCtrl.dish.comments | orderBy:searchBox track by $index">
<blockquote>
<p>{{dishcomment.rating}} Stars</p>
<p>{{dishcomment.comment}}</p>
<small>{{dishcomment.author}}
{{dishcomment.date | date:'MMM.dd, yyyy'}}</small>
</blockquote>
【问题讨论】:
-
除非我完全误解了这个问题,否则这是写的:plnkr.co/edit/poU4KXsy9GZFeLjnnsZ6。键入
rating会导致顺序发生变化,键入comment或author也是如此。 -
这不是一个想法。我只想调用一个接收输入值的函数并检查是否是一个选项顺序 ng-repeat。
-
您要针对哪个属性检查文本框内容?
标签: javascript angularjs angularjs-ng-repeat ng-repeat