AngularJS - Using a Service as a Model, ng-repeat not updating

后端 未结 2 1278
失恋的感觉
失恋的感觉 2020-12-13 11:00

I\'m creating an ajax search page which will consist of a search input box, series of filter drop-downs and then a UL where the results are displayed.

As the filters

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 11:47

    In your HTML, you need to reference a property defined on your controller's $scope. One way to do that is to bind $scope.searchService.results to searchService.results once in your controller:

    $scope.searchService.results = searchService.results;
    

    Now this line will work:

  • In your service, use angular.copy() rather than assigning a new array reference to results, otherwise your controller's $scope will lose its data-binding:

    var new_results = [{ 'title': 'title 3' }, 
                       { 'title': 'title 4' }];
    angular.copy(new_results, results);
    

    Fiddle. In the fiddle, I commented out the initial call to find(), so you can see an update happen when you type something into the search box.

提交回复
热议问题