【问题标题】:multiple values in query in angularjs / Restangularangularjs / Restangular中查询的多个值
【发布时间】:2014-04-10 09:21:24
【问题描述】:

我想过滤数据,从api获取数据,但是不行,有什么解决办法吗?

$scope.filter_all2 = function(value) {
    $scope.filtered_inventories = new Inventory().query({model: value, manufacturer: value}).$object;
};

我的模板

<select ng-model="inventory.model" ng-change="filter_all2(inventory.model)">
    <option value="">Model</option>
    <option ng-repeat="inventory in inventories | unique:'model'" value="{{inventory.model}}">{{inventory.model}}</div>
</select>

<select ng-model="inventory.manufacturer" ng-change="filter_all2(inventory.manufacturer)">
    <option value="">Manufacturer</option>
    <option ng-repeat="inventory in inventories | unique:'manufacturer'" value="{{inventory.manufacturer}}">{{inventory.manufacturer}}</div>
</select> 

编辑:

Tastypie api,信息。

class InventoryResource(ModelResource):
assigned = fields.ForeignKey('bos_inventory.api.AssignedResource', 'assigned', full=True, null=True)
location = fields.ForeignKey('bos_inventory.api.LocationResource', 'location', full=True, null=True)
tags = fields.ToManyField(TagResource, 'tags', full=True, null=True)

class Meta:
    queryset = Inventory.objects.all()
    resource_name = 'inventory'
    list_allowed_methods = ['get', 'put', 'post', 'delete', 'copy']
    detail_allowed_methods = ['get', 'put', 'post', 'delete', 'copy']
    authorization = DjangoAuthorization()
    authorization = Authorization()
    serializer = Serializer()
    filtering = {'id': ALL, 'barcode': ALL, 'model': ALL,  'manufacturer': ALL, 'location': ALL, 'tags': ALL, 'assigned': ALL, 'inventory': ALL}

【问题讨论】:

  • 您必须在服务器上过滤查询。服务器需要解释查询参数并返回过滤结果。这与javascript、angularjs、api、restangular无关

标签: javascript angularjs api restangular


【解决方案1】:

Angular 不提供内置的“唯一”过滤器。你可以使用angularUI独特的过滤器来解决这个问题。

app.js

angular.module("myApp",['ui.utils'])
.controller("myCtrl",function($scope){
  //fake data
  $scope.inventories = [{model:"a model",manufacturer:"a manufacturer"},{model:"a model",manufacturer:"b manufacturer"},{model:"b model",manufacturer:"b manufacturer"},{model:"b model",manufacturer:"a manufacturer"}];

  $scope.filter_all = function(value){
    alert(value);
    //put ur RESTful service call here
  }
});

app.html

<script src="http://angular-ui.github.io/ui-utils/dist/ui-utils.js"></script>
<div ng-controller="myCtrl">
  {{inventory.model}}//{{inventory.manufacturer}}

  <select ng-model="inventory.model" ng-change="filter_all(inventory.model)" ng-options="inventory.model as inventory.model for inventory in inventories | unique: 'model'">
    <!--prompt option-->
    <option value="">Model</option>
  </select>

  <select ng-model="inventory.manufacturer" ng-change="filter_all(inventory.manufacturer)" ng-options="inventory.manufacturer as inventory.manufacturer for inventory in inventories | unique: 'manufacturer'">
    <option value="">Manufacturer</option>
  </select> 
</div>

请注意,当您希望选择模型绑定到非字符串值时,请使用 ng-options,而不是使用 ng-repeat 生成动态选项。

这是jsFiddle DEMO

【讨论】:

  • 这里我说的不是唯一性,而是如何过滤数据、向服务器发送查询并只接收选定的信息。我有独特的指令。
  • 那么......你的 api 调用问题是什么。无法向服务器发送 GET 请求?得到意外的服务器响应?控制台日志有任何错误吗?您可以发布您的库存服务实施吗?
  • 我 dont get any errors,it just dont 工作,如果我输入 2 个或更多值,它会工作,如果我输入一个值,它会得到选定的值。
  • 能否提供自定义服务Inventory实现? .factory('Inventory',['Restangular'],function(){....}
猜你喜欢
  • 1970-01-01
  • 2014-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-28
相关资源
最近更新 更多