$ resouce响应数据未在html上更新

时间:2016-04-24 23:40:30

标签: angularjs ionic-framework

我无法在UI上更新金额,但是当我查看网络时,我能够看到响应值。我的代码有什么问题,我不熟悉$ resource和angularjs的新东西。

 <ion-view title="HSF" id="page4">
        <ion-content overflow-scroll="true" padding="true" class="has-header">
            <h3 id="hSF-heading5" style="color:#000000;text-align:center;">Summary</h3>

            <ion-scroll>
                <ul class="list" data-ng-repeat="mth in monthList()">
                    <li class="item item-positive" ng-click="">
                        {{mth.label}}
                        <span style="float: right;">Total: ${{mth.amount}}</span>
                    </li>
                </ul>
            </ion-scroll>
        </ion-content>
    </ion-view>


    .factory('RESTService',['$resource', function($resource){
            return $resource('http://xxx/' + 'transaction', {id: '@id'},{
                readSales: {
                    method: 'POST',
                    isArray: false
                }
            });
        }])





    .controller('HSFCtrl', function($scope, $http, $state, RESTService) {

        //region YearlySales
        var list = [];
        var today = new Date();
        var yearIndex = today.getFullYear();
        var yearObj = {
            label: "",
            amount:0
        };
        yearObj.label = yearIndex;
        $http({
            url: 'http://xxx/' + 'transaction',
            data: {"type": 3, "datetime": yearObj.label},
            method: 'POST'
        }).success(function (response) {
            yearObj.amount = response;
        });
        list.push(yearObj);
        $scope.yearList = list;
    //endregion


        //region MonthlySales
        $scope.monthList = [];
        var months = [1,2,3,4,5,6,7,8,9,10,11,12,13];
        var monthNames = [ 'January', 'February', 'March', 'April', 'May', 'June',
            'July', 'August', 'September', 'October', 'November', 'December' ];
        for(var mth in months){
            $scope.amount = {};
            var monthObj = {
                label: "",
                amount: 0
            };
            var objToSend = {
                type: 2,
                datetime: '2016' + '-' + mth
            };
            //console.log(mth);
            var read = RESTService.readSales(objToSend);
            read.$promise.then(function(result){
                monthObj.amount = Number(result);
            });


            monthObj.label = monthNames[mth];
            $scope.monthList.push(monthObj);
        }
        //console.log($scope.monthList);
        //endregion


        $scope.goToMSP = function(){
            $state.go('tabsController.MSP');
        }

    });

2 个答案:

答案 0 :(得分:0)

您可以尝试围绕新的响应数据设置角度应用,这会强制摘要:

$scope.$apply(function() {
  $scope.yearList = list;
});

更多信息https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$digest

答案 1 :(得分:0)

响应对象具有以下属性:

  

data - {string | Object} - 用。转换的响应体   转换函数。

所以你必须在你的变量中传递响应,如下所示:

yearObj.amount = response.data;