【发布时间】:2016-08-07 21:29:27
【问题描述】:
我看到了一些其他帖子,我认为这些帖子可能会有所帮助,但没有任何运气。当我静态指定要过滤的属性时,我有一个可以正常工作的过滤器。我正在构建一个角度组件,我不知道哪些数据将实际传递到组件中。例如,这里有一些示例数据:
this.customers = [
{ name: 'Jim', city: 'Minneapolis', state: 'MN', zip: 44332 },
{ name: 'Boe', city: 'Scottsdale', state: 'AZ', zip: 44332 },
{ name: 'Tom', city: 'S.F.', state: 'CA', zip: 11223 },
{ name: 'Joe', city: 'Dallas', state: 'TX', zip: 34543 },
{ name: 'Jon', city: 'L.A.', state: 'CA', zip: 56433 }
];
我的静态过滤器效果很好:
public filterTextChangeLocal($event: ng.IAngularEvent) {
if (this.itemDisplayProperty = "name") {
this.filteredItems = this.$filter("filter")(this.items, {name: this.ngModelValue});
} else if (this.itemDisplayProperty = "city") {
this.filteredItems = this.$filter("filter")(this.items, {city: this.ngModelValue});
} else if (this.itemDisplayProperty = "state") {
this.filteredItems = this.$filter("filter")(this.items, {state: this.ngModelValue});
} else if (this.itemDisplayProperty === "zip") {
this.filteredItems = this.$filter("filter")(this.items, {zip: this.ngModelValue});
}
}
问题是组件的用户可以传入任何类型的数据,其属性可能完全不同,所以我需要一些可以解释任何指定属性的东西。我有一个名为“itemDisplayProperty”的隔离范围属性,它允许用户从他们的数据中指定他们想要在下拉列表中显示的属性,这就是我需要过滤的属性。他们可以说,“item-display-property="address",地址属性中的数据将显示在下拉列表中。我试过了,但由于不允许使用多个“this”字样,所以不起作用:
this.filteredItems = this.$filter("filter")(this.items, {
this.itemDisplayProperty : this.ngModelValue
});
这是我的输入和下拉列表供参考:
<input type="text" class="form-control"
ng-change="ctrl.filterTextChangeLocal($event)"
ng-model="ctrl.ngModelValue"
ng-click="ctrl.openDropdown($event)" />
<ul class="dropdown-menu list-group" ng-if="!ctrl.ngDisabled">
<li class="list-group-item"
ng-repeat="row in ctrl.filteredItems"
ng-mousedown="ctrl.onSelectedLocal(row, $event)">
{{row[ctrl.itemDisplayProperty]}}
</li>
</ul>
【问题讨论】:
标签: javascript angularjs filter angularjs-ng-repeat angular-ngmodel