Data Binding does not work after calling function

Asked

Viewed 367 times

1

I have the following function:

$scope.calcularTotal = function(startDate, endDate){
    $scope.items = $filter('betweenDate')($scope.items, 'dataPagamento', startDate, endDate);
    console.log("testee: "+$scope.items)
    var total = 0;
    var glosa = 0;
    var lote = 0;
    angular.forEach($scope.items, function(item) {
      total += (item.totalLiquido);
      glosa +=(item.totalGlosa);
      lote +=(item.totalLote);
    });
    $scope.totalLiquido = total;
    $scope.totalGlosa = glosa;
    $scope.totalLote = lote;
    console.log("Total: "+$scope.total)
    $state.go('tabs.facts', {}, {reload: true});
  }

In this function I go through an Array and sum the values. In console.log that has at the end the total appears correctly. However when I try to do the Data Binding on my page the values do not appear:

This is the page:

ion-view title="Totais" cache-view="false">
  <ion-content has-header="true" padding="true">
    <h3 style="text-align:center">Total</h3>
    <p style="margin-top:30px;font-weight:bold;color: #0066FF">Total Lote:    {{totalLote | currency}}</p>
    <p style="font-weight:bold;color: #990000">Total Glosa: {{totalGlosa | currency}}</p>
    <p style="font-weight:bold;color: #339900">Total Líquido: {{totalLiquido | currency}}</p>
  </br>
</br>
</br>
</br>
<p>
  <a class="button icon ion-home" href="#/tab/home" style="width:100%"> Home</a>
</p>
</ion-content>
</ion-view>

And that’s the result:

inserir a descrição da imagem aqui

How could I solve this problem?

  • You printed only $Scope.total. Put the other values also

  • I put and all are printed on the console @Emirmarques. However they do not appear on the page

  • At a glance: http://i.stack.Imgur.com/8k3dg.png

  • Try without the filter currency

  • Also not working @Emirmarques

  • The strange thing is that when I put this bit inside the calculator functionTotals inside another function that I have to load along with the page works, but does not do what I want.

  • I think the problem is to accomplish all this when I click on a button and already move to another page. Maybe the page is not being updated :/

  • Reload : true is not forcing reload? Pass as false

  • Nothing has changed. Another strange fact, when I declare the variables at the beginning of the page, ex: $Scope.totalLiquido = ""; appears the value 0 dai.

Show 4 more comments

1 answer

1

Tips:

1) You must understand the Angular Digest cycle;

2) You should call this function somewhere (you didn’t detail it in the question) either by clicking a button or as code in the controller;

3) When you change Scope variables within the function, these changes happened at a time later than the initial Digest in the view load, then you should warn Angular to re-escan the changes. Something like: if (!$scope.$$phase) $scope.$apply(); in the last row of its function.

Browser other questions tagged

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