简体   繁体   English

如何使用json对象显示angularjs ng-repeat

[英]How to display angularjs ng-repeat with json object

I'm trying to do a search with product results using AngularJS. 我正在尝试使用AngularJS搜索产品结果。 I've obtained an JSON object with my results": example below 我已经获得了带有结果的JSON对象”:以下示例

[{"store_id":"17","user_id":"29","company_name":"Liquor R Us","company_type":"Alcohol",
"phone":"(303) 555-5555","website":"http:\/\/liqourrus.com","address":"5501 Peoria St",
"address_2":"","city":"Denver","state":"CO","zip":"80239","lat":"39.796181",
"lng":"-104.84863","mon_open":"","mon_close":"","tues_open":"","tues_close":"",
"wed_open":"","wed_close":"","thurs_open":"","thurs_close":"","fri_open":"","fri_close":"",
"sat_open":"","sat_close":"","sun_open":"","sun_close":"","distance":"1.1668156112981596",
"s":"s","st":"Every Store"}]

I'm using ng-repeat="store in storeResult" and the results will not display until I click on my Filter function, please note the filter is not applied! 我使用的是ng-repeat="store in storeResult" ,在单击“过滤器”功能之前,结果不会显示,请注意该过滤器未应用!

<div class="product" ng-repeat="store in storeResult">
    {{store.company_name}}
</div>

ng-click="setFilter('all');"

$scope.setFilter = function(filter) {
    if(filter == 'all') {
        $scope.searchProduct.product_type = '';
        $scope.searchStore.company_type = '';
    }
}

If I click the "setFilter" button, all results show. 如果单击“ setFilter”按钮,将显示所有结果。 I'm trying to figure out how to make it display without having to click the button. 我试图弄清楚如何使其显示而不必单击按钮。

This work around only works in Chrome. 解决方法仅适用于Chrome。 Firefox and IE, never display results. Firefox和IE,永远不会显示结果。

Plunker: link 柱塞: 链接

My best guest is that by clicking the setFilter button you are triggering a digest cycle and your ngRepeat will be executed because of that. 我最好的客人是,通过单击setFilter按钮,您将触发一个摘要循环,因此您的ngRepeat将被执行。 I suspect that you are assigning your storeResult outside the digest cycle and that's the reason is not displaying initially. 我怀疑您在摘要周期之外分配了storeResult ,这就是最初没有显示的原因。 I cannot tell for sure because is not in your description how is that JSON assigned to your storeAssignment . 我不能肯定地说,因为您的描述中没有JSON如何分配给storeAssignment。

Can you check how is that variable assigned? 您可以检查该变量的分配方式吗?

Can you change the way it's and do this: 您可以更改其方式并执行以下操作:

$timeout(function() {
    $scope.storeAssignment = _your_json_value;
}); 

You will have to include the $timeout dependency but with this, a change in storeAssignment will be inside your digest cycle and the ngRepeat will see that change. 您将必须包括$timeout依赖关系,但是与此相关的是,storeAssignment中的更改将在摘要周期内,并且ngRepeat将看到该更改。

Hope that helps. 希望能有所帮助。

It looks like you're not setting the filter to 'all' until you click the filter function. 好像您没有将过滤器设置为“全部”,直到您单击过滤器功能。 Make sure that filter starts out as 'all'. 确保filter开始为“全部”。

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

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