模型更改后ngRepeat不更新 [英] ngRepeat not updating after model changed

查看:132
本文介绍了模型更改后ngRepeat不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我特林code稍微搜索输入,从使用ngResource数据库获取数据。

I'm tring to code a little search input to get data from a database using ngResource.

数据显示在一个NG重复的页面,但是当我做搜索和$范围已被更新,视图不更新,并显示旧数据。

the data are shown in the page with a ng-repeat, but when i do the search and the $scope has been updated, the view is not updated and show old data.

下面是code:

main.html中(活动视图)

main.html (active view)

<div ng-controller="searchCtrl as searchCtrl" layout="column">
    <form class="form-inline search-form col-md-offset-1">
        <div class="form-group col-md-5">
            <label class="sr-only" for="search_location">Location</label> <input
                id="search_location" type="search" class="form-control"
                ng-Autocomplete ng-model="place" placeholder="Location" />
        </div>
        <div class="form-group col-md-5">
            <label class="sr-only" for="search_tags">Tags</label> <input
                style="width: 100%;" id="search_tags" type="search"
                class="form-control" id="search_tags" placeholder="Tags">
        </div>

        <div class="col-md-1">
            <md-button class="md-fab md-mini" aria-label="Search" ng-click="searchCtrl.search()"> <md-icon class="md-fab-center"
                md-font-icon="glyphicon glyphicon-search" style="color: black;"></md-icon>
            </md-button>
        </div>
    </form>
</div>

<div ng-controller="mainCtrl">

    <div ng-repeat="evento in eventi" ng-include="'views/components/event_card.html'" class="col-md-3"></div>

</div>

main.js

main.js

'use strict';
app.factory('Eventi', function($resource) {
    return $resource('/eventsws/events/:location', {location : '@location'}, { 
        search: {
            method: 'GET',
            params: {
                'location' : "@location"
            },
            isArray : true
        }
    })
});

app.controller('mainCtrl', function($scope, Eventi) {
    $scope.eventi = Eventi.query();
});

searchbar.js

searchbar.js

'use strict';
app.controller('searchCtrl', function($scope, Eventi) {
    $scope.place = null;

    this.search = function() {

        $scope.eventi = Eventi.search({
            'location' : $scope.place
        });
    }
});

当它开始它从数据库中获取的所有数据并正确显示这些,当我尝试做一个搜索时,$ scope.eventi进行更新(我可以看到从调试到$ scope.eventi新的数据)但视图仍显示旧的数据,而不会更新。

when it start it get all the data from the database and display them correctly, when i try to make a search, the $scope.eventi is updated (i can see the new data in $scope.eventi from the debug) but the view still show the old data and never update.

我试着使用$范围。$适用于搜索功能的结束,但结果是一样的。

I've tried to use $scope.$apply at the end of the search function but the result is the same.

有你,为什么它不工作任何想法?

Have you any idea why it's not working?

感谢您的时间。

推荐答案

在$ scope.eventi你看到的调试是一个在你searchCtrl,而不是一个来自你的mainCtrl。要更新您的mainCtrl $ scope.eventi你必须找到一个办法了。

The $scope.eventi you see in the debug is the one in your searchCtrl and not the one from your mainCtrl. To update your mainCtrl $scope.eventi you have to find an other way.

一个干净的,但长期的解决办法是在控制器中使用到的变量共享服务。

A clean but long solution would be using services to shares variables in your controllers.

要回答这个问题的意见:

To answer the question in comments :

我可以看到它更新,但认为仍显示旧的数据

i can see it updated, but the view still show the old data

我想什么问题(即使我居然没看到你的code)。

I guess what's the problem (even if i actually didn't see your code).

如果您绑定您的VAR是这样的:

If you bind your var like this :

服务

[...]
service.serviceVar = 1;
return service
[...]

这将创建一个参考1变种。

This will create a "1" var with a reference.

控制器

  [...]
  $scope.myvar = Service.serviceVar;
  [...]

这将绑定$ scope.myvar到1的参考。

This will bind $scope.myvar to the "1" reference.

如果你在你的服务或在其他控制器做到这一点:

If you do this in your service or in an other controller :

   service.serviceVar = 2;

您将创建一个新变种2一个新的参考,你将赋予该参考service.serviceVar。严重到老1 VAR所有的旧的引用将不会更新。

You will create a new var "2" with a new reference and you will assign this reference to service.serviceVar. Badly all your old references to the old 1 var will not update.

要避免做这样的:

服务

[...]
service.servicevar = {};
service.servicevar.value = 1;
return service
[...]

您创建一个新的参考对象并将其分配给servicevar。
您创建一个变种1,并将其分配给servicevar.value。

You create an object with a new reference and assign it to servicevar. You create a var "1" and assign it to servicevar.value.

控制器

[...]
$scope.myvar = Service.servicevar;
[...]

您指定servicevar参考你的范围变种。

You assign the servicevar reference to your scope var.

查看

{{myvar.value}}

您可以通过使用VAR的财产使用的值。

You can use the value by using the property of your var.

更新VAR这样做:

service.servicevar.value = 2;

您将创建一个新变种2一个新的参考,并取代这一个老参考。

You will create a new var "2" with a new reference and replace the old reference by this one.

但这次你保持servicevar所有的引用在控制器。

BUT this time you keep all your references to servicevar in your controllers.

我希望我是清楚的,并回答你的问题。

I hope i was clear and it answer your question.

编辑:

尝试从来没有使用$范围。$适用。这是一个非常不好的做法。如果你用它来使一些工作,你应该找一个其他的做到这一点(这将是对堆栈一个很大的问题我想:为什么我需要$适用于解决我的问题XXXXX)

Try to never ever use $scope.$apply. It's a really bad practice. If you use that to make something works, you should probably find an other to do that (And it will be a great question for Stacks i guess : "Why do i need $apply to solve my problem XXXXX")

rsnorman15有关于你的异步调用的使用好点。看看他的回答了。

rsnorman15 has a good point about your uses of asynchronous calls. Take a look at his answer too.

下面是使用服务共享的属性我老plunker之一

Here is one of my old plunker using a service to share properties

这篇关于模型更改后ngRepeat不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆