如何在传递$ scope之前编辑数据?

时间:2015-07-08 16:29:30

标签: json angularjs service controller scope

我的控制器有问题。

消费者服务,当我获得数据时,“数据”会在列表中显示它们,一切正常。

.controller('AccountsCtrl', function($scope, getAccountsData){
        getAccountsData.resp.query(function(data){

        $scope.accounts = data;

        },function(dataError){

        var alertPopup = $ionicPopup.alert({
            title: 'Login failed!',
                        template: 'Please check your credentials!' + JSON.stringify(dataError)
        });
    });
    }) 

但是当我想在传递给“$ scope”之前编辑“数据”时,我看不到列表中的任何内容。

.controller('AccountsCtrl', function($scope, getAccountsDataServices){
        getAccountsDataServices.resp.query(function(data){

             for(var i in data){

            if(data[i].account.type.id == '17'){
                data[i].status.formattedReservedAmount = "texto editado";
                 };
            };

        $scope.accounts = data;

        },function(dataError){

        var alertPopup = $ionicPopup.alert({
            title: 'Error!',
            template: 'Please check!
        });
    });
    }) 

---------------查看------------------               

            <div class="name">{{ account.account.type.name }}</div>
            <div class="diner">{{ account.status.formattedReservedAmount }}</div>
          </ion-item>
        </ion-list>

收到的数据是:

[{"account": {
            "id": 913,
            "type": {
                "id": 17,
                "name": "account-1"
            },
            "default": true
        },
        "status": {
            "formattedReservedAmount": "$ 0,00 Pesos"
        }
    },
    {
        "account": {
            "id": 4050,
            "type": {
                "id": 35,
                "name": "account-2"
            },
            "default": false
        },
       "status": {
            "formattedReservedAmount": "$ 0,00 Pesos"
        }}]

2 个答案:

答案 0 :(得分:0)

结果上的forEach应该可以解决问题。

.controller('AccountsCtrl', function($scope, getAccountsDataServices){
    getAccountsDataServices.resp.query(function(data){
        angular.forEach(data, function(a) {
            if (a.account.type.id === 17) {
                a.account.status.formattedReservedAmount = 'texto editado';
            }
        });

        $scope.accounts = data;
    }, function(error) {
        $ionicPopup.alert({
            title: 'Error!',
            template: 'Please Check!'
        })
    });
});

答案 1 :(得分:0)

.controller('AccountsCtrl', function(`$scope`, getAccountsDataServices){
getAccountsDataServices.resp.query(function(data){   
`$scope.accounts` = data.map(function (c) {      
          if (c.account.type.id == '17'){
             c.status.formattedReservedAmount = ':DDDD';
          }
           return c;
    });
    }, function(error) {
    $ionicPopup.alert({
            title: 'Error!',
            template: 'Please Check!'
        })
    });
});