Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
796 views
in Technique[技术] by (71.8m points)

angularjs - ngRepeat not updating after model changed

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

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.

Here is the code:

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

'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

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

    this.search = function() {

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

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?

Thanks for your time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

Problem

If you bind your var like this :

Service

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

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

Controller

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

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

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

   service.serviceVar = 2;

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.

Solution

To avoid that do it like this :

Service

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

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

Controller

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

You assign the servicevar reference to your scope var.

view

{{myvar.value}}

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

Updating the var doing this :

service.servicevar.value = 2;

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

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

I hope i was clear and it answer your question.

EDIT :

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 has a good point about your uses of asynchronous calls. Take a look at his answer too.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...