简体   繁体   English

AngularJS过滤JSON / Object的属性

[英]AngularJS Filter on property from JSON/Object

I am new to Angular and I am building a simple list (Also with ionic, but its using angular) of ice creams. 我是Angular的新手,正在建立一个简单的冰淇淋列表(也使用离子型,但使用了angular)。 I get the data from a JSON file with objects (an object for each ice cream). 我从带有对象(每个冰淇淋的对象)的JSON文件中获取数据。 All objects have a property called "beschikbaar" what will be set to "Ja" or "Nee" (can also be true or false if thats making the job easier to do). 所有对象都有一个名为“ beschikbaar”的属性,该属性将设置为“ Ja”或“ Nee”(如果这样会使工作更容易进行,也可以为true或false)。 In the list I made with a ng-repeat I would like to show only the items where the property "beschikbaar" is set to "Ja" or true. 在用ng-repeat进行的列表中,我只想显示属性“ beschikbaar”设置为“ Ja”或true的项目。 Otherwise I would like to hide them. 否则我想隐藏它们。 I can't get it done. 我做不完

Is it possible to fix this with just a simple filter in the view or must there be a custom filter to do this? 是否可以仅使用视图中的简单过滤器来解决此问题,还是必须有一个自定义过滤器才能做到这一点? I have no exp. 我没有经验 with custom filters. 与自定义过滤器。

I have tried all kind of things, the filter in the view now is just an example of a try. 我已经尝试了所有事情,现在视图中的过滤器只是一个尝试示例。

My code 我的密码

VIEW 视图

     <ion-item ng-repeat="item in icecreams | filter: {item.beschikbaar}:Ja" class="item-thumbnail-left" href="#">
        <img src="http://placehold.it/100x100">
        <h2>{{ item.name }}</h2>
        <p>{{ item.info }}</p>
        <h4>{{ item.type }}</h4>
        <h4>In de winkel tot: {{ item.end_date }}</h4>
      </ion-item>

JSON JSON格式

[

        {
            "name" : "Aardbei Sorbet",
            "type" : "Sorbet",
            "info" : "Aardbei sorbet ijs is een frisse en zoete ijssmaak. Heerlijk voor in de zomer met bijvoorbeeld slagroom en chocolade ijs. Gemaakt met Hollandse Aardbeien.",
            "end_date" : "Maandag 17 Juli",
            "ingre" : "Hollandse aardbeien, melk, water, vanille roomijs",
            "suikervrij" : "Nee",
            "gluten" : "Nee",
            "beschikbaar" : "Ja"             
        },

        {
            "name" : "Mango Sorbet",
            "type" : "Sorbet",
            "info" : "Mango sorbet ijs is een exotische smaak. Met dit ijs krijg je het gevoel alsof je in een ver en exotisch land bent. Gemaakt met echte mango. Lekker te combineren met Ananas ijs.",
            "end_date" : "Maandag 17 Juli",
            "ingre" : "Mango stukjes, citroensap, water, vruchtvlees ananas",
            "suikervrij" : "Nee",
            "gluten" : "Ja",
            "beschikbaar" : "Ja"                        
        },

        {
            "name" : "Tompouchen ijs",
            "type" : "Special",
            "info" : "Een hema naast de winkel is garantie voor lekker ijs. Tompouchen ijs is een leuk experiment geworden. De romige smaak komt er in terug en ik heb er voor gezorgd dat hij niet extreem zoet is!",
            "end_date" : "Maandag 17 Juli",
            "ingre" : "Tompouchen, cakebeslag, room, melk, water",
            "suikervrij" : "Nee",
            "gluten" : "Ja",
            "beschikbaar" : "Ja"             
        },

        {
            "name" : "Drop ijs",
            "type" : "Snoep",
            "info" : "Ijs van drop, hoe tof is dat? Zoet van smaak, met een authentieke drop smaak. Gemaakt op basis van drop en vanille ijs.",
            "end_date" : "Maandag 17 Juli",
            "ingre" : "Tompouchen, cakebeslag, room, melk, water",
            "suikervrij" : "Nee",
            "gluten" : "Ja",
            "beschikbaar" : "Nee" 
        }

]

Controller 控制者

.controller('MainController', ['$scope', '$http', function($scope, $http){

  $http.get('js/data.json').success(function(data) {
    $scope.icecreams = data;
  });

}]);

I know there are a lot of questions of this subject but I just can't get it to work. 我知道这个主题有很多问题,但我无法解决。 Thanks for helping out! 感谢您的帮助!

You could achieve this without any filters, just use an ng-if . 您可以使用任何ng-if来实现,而无需任何过滤器。

<ion-item ng-repeat="item in icecreams" class="item-thumbnail-left" href="#" ng-if="item.beschikbaar == 'Ja'">
        <img src="http://placehold.it/100x100">
        <h2>{{ item.name }}</h2>
        <p>{{ item.info }}</p>
        <h4>{{ item.type }}</h4>
        <h4>In de winkel tot: {{ item.end_date }}</h4>
</ion-item>

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

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