ng-repeat数组第一次更新表数据我选择了值,但它只更新了表数据一次

时间:2017-01-07 08:34:57

标签: angularjs ionic-framework

我尝试根据select选项更新表格视图。表格视图只更新一次,当我第二次选择该选项时,视图没有更新,我没有得到问题所在。请帮我解决这个问题..

这是app.js

$scope.User = {};

    $scope.arr = [];
    $scope.loaddata = function(User) {
    $scope.User.site = layouts;
    AllServices.teamAllDataFunction1(User)
            .then(function(response) {
    $scope.User.data=response.data;
    });

    };
    $scope.getdatalayoutwise = function(User) {
            var total = 0;
            var total1 = 0;
            for (var i = 0; i < ($scope.User.data).length; i++) {

                if($scope.User.data[i].Layout == $scope.User.selectedSite) {
                    total += parseInt($scope.User.data[i].dp_inst_pending);
                    $scope.arr.push($scope.User.data[i]);
                }
            }

            for (var j = 0; j < ($scope.User.data1).length; j++) {
                if($scope.User.data1[j].Layout == $scope.User.selectedSite) {
                    total1 += parseInt($scope.User.data1[j].DP_Inst_Pending);
                }
            }

            $scope.User.teamTotal = total;
            $scope.User.personalTotal = total1;

            $scope.data = [$scope.User.teamTotal, $scope.User.personalTotal];
            $scope.totamnt =  parseInt($scope.User.personalTotal) + parseInt($scope.User.teamTotal);
            $scope.User.totalamount = $filter('translate')('totalpending') + ": " + $filter('currency')($scope.totamnt, "");
            $scope.User.data = $scope.arr;
        };

这里是html

<select name="site" ng-model="User.selectedSite" ng-change="getdatalayoutwise(User)">
                    <option value="">--{{'selectsite_message' | translate}}--</option>
                    <option ng-repeat= "option in User.site" value="{{option.Layout}}">{{option.Layout}}</option>
                </select>

<table ng-table>
                <tr>
                    <th>advisor_name</th>
                    <th>totalpending</th>

                </tr>

                <tr ng-repeat="data in User.data | filter : {Layout: User.selectedSite}: true" ng-if="data.dp_inst_pending">
                    <td class="ui-helper-center"><a ng-click="advisorDetails($index, data, User)">{{data.AdvisorName}}</a></td>
                    <td>{{data.dp_inst_pending | currency:"&#8377;":0}}</td>   
                </tr>
            </table>

4 个答案:

答案 0 :(得分:0)

您需要使用$scope.$apply()

$scope.getdatalayoutwise = function(User) {

   $scope.$apply(function () {
      var total = 0;
      var total1 = 0;
      for (var i = 0; i < ($scope.User.data).length; i++) {

          if($scope.User.data[i].Layout == $scope.User.selectedSite) {
             total += parseInt($scope.User.data[i].dp_inst_pending);
             $scope.arr.push($scope.User.data[i]);
          }
      }
      ...
   }); 
}

documentation

答案 1 :(得分:0)

将您的功能更改为此

$scope.loaddata = function(User) {
    $scope.User.data = [];
    $scope.User.site = layouts;
    AllServices.teamAllDataFunction1(User)
            .then(function(response) {
    $scope.User.data=response.data;
});

并添加ng-if

<table ng-table ng-if="User.data.length">
    <tr>
        <th>advisor_name</th>
        <th>totalpending</th>
    </tr>
    <tr ng-repeat="data in User.data | filter : {Layout: User.selectedSite}: true" ng-if="data.dp_inst_pending">
        <td class="ui-helper-center"><a ng-click="advisorDetails($index, data, User)">{{data.AdvisorName}}</a></td>
        <td>{{data.dp_inst_pending | currency:"&#8377;":0}}</td>   
     </tr>
</table>

答案 2 :(得分:0)

将此作为 getdatalayoutwise()函数中的第一行添加:

$scope.arr = [];

答案 3 :(得分:0)

只需执行以下操作即可实现

$scope.safeApply = function(fn) {
        var phase = this.$root.$$phase;
        if(phase == '$apply' || phase == '$digest') {
            if(fn && (typeof(fn) === 'function')) {
                fn();
            }
        } else {
            this.$apply(fn);
        }
    };

$scope.getdatalayoutwise = function(User) {
var total = 0;
      var total1 = 0;
      for (var i = 0; i < ($scope.User.data).length; i++) {

          if($scope.User.data[i].Layout == $scope.User.selectedSite) {
             total += parseInt($scope.User.data[i].dp_inst_pending);
             $scope.arr.push($scope.User.data[i]);
          }
      }
      ...
$scope.safeApply (function () {
$scope.User.data = $scope.arr;
   }); 
};