Search code examples
angularjsionic-frameworkionic-view

Filter baseaded in .state ID field in Ionic


Filter baseaded in .state ID field.

I have this state:

   .state("nhaac.ofertas_restaurante", {
    url: "/ofertas_restaurante/:cadastra_oferta_cod_fornecedor",
    cache:false,
    views: {
        "nhaac-side_menus" : {
                    templateUrl:"templates/ofertas_restaurante.html",
                    controller: "restaurantePromocoesCtrl"
                },

Where I get an ID from a Json file (or Object array).

And I want get this field ID and filter in a View, listing only registers that contain the ID from the state:

ng-repeat="item in ofertass | filter:????????"

How I can do this?


Solution

  • You can grab the dynamic variable of your state url with $stateParams in your controller. In this case its $stateParams.cadastra_oferta_cod_fornecedor since you've declared the state url to be /ofertas_restaurante/:cadastra_oferta_cod_fornecedor. Use this to create a custom filter where you're matching the ids.

    Controller

    .controller('Ctrl', function($scope, $stateParams) {
      angular.extend($scope, {
        filterById: function(item) {
          return item.cadastra_oferta_cod_fornecedor === $stateParams.cadastra_oferta_cod_fornecedor;
        }
      });
    });
    

    View

    <div ng-repeat="item in ofertass | filter:filterById(item)"></div>