Update ng-repeat outside ng-view?

Asked

Viewed 382 times

2

I have a fixed menu at the top, where the pages are opened through Routes in ng-view.

In an X view I add a new element to my database, and in the fixed menu I use ng-repeat to list these elements.

When I add something new in the database this ng-repeat fixed menu is not updated, only if using F5.

Is there any way to update this ng-repeat that is in the fixed menu (outside ng-view) without using F5?

2 answers

2

A possible model may implement a service that is responsible for containing the collection, displayed via a member.

Inject this service in controller that feeds its view, and binds this member to $scope of your view containing the ng-repeat.

Use a service method to force the list update whenever necessary.

2


I agree 100% with the above colleague, follow a simple example below:

 var servico1 = function() { 
      var servico = this; 
      servico.listaInserido = [];

      servico.add = function(item) { servico.listaInserido.push(item) }
 }
 var controllerfixo = function($scope, servico1) { $scope.servico = servico1; }
 var controllerrelativo = function($scope, servico1) { $scope.servico = servico1; }

 var app = angular.module('app');
 app.service('servico1', servico1);
 app.controller('controlerfixo', controllerfixo);
 app.controller('controllerrelativo', controllerrelativo);

HTML

<ul class="fixo" ng-controller="controllerfixo">
   <li ng-repeat="item in servico.listaInserido">{{item}}</li>
<ul> 

<div ng-controller="controllerrelativo">
     <button ng-click="servico.add({ a: 1})">add</button>
</div>
  • Thanks for the suggestion let’s see what happens.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.