如何在http请求后更新ng-repeat列表

时间:2016-05-31 20:40:40

标签: javascript angularjs

执行PUT请求后,我无法更新ng-repeat列表。服务很好。

controller.js

teamData.updateTeam(team.id, teamObj, function(res) {
                        console.log('Success');
                    });

service.js

teamService.updateTeam = function(teamId, param, callback) {
          var req = {
            method: 'PUT',
            url: '/team' + teamId,
            headers: {
              'Content-Type': 'application/json'
            },
            'data': param
          };
          return $http(req).then(function(res){
            callback(res);
          }, function(err){
            callback(err);
          });
        };

teamRoute.js

app.put('/team/:id', function(request, response) {
    var options = {
        host: reqParam.hostFramework,
        path: reqParam.path + '/team/' + request.params.id,
        method: 'PUT',
        headers: {
            'token': reqParam.token,
            'Content-Type': 'application/json',
            'Content-Length': Buffer.byteLength(JSON.stringify(request.body))
        }
    };
    var resString = '';
    var req = https.request(options, function(res) {
        res.setEncoding('utf8');
        res.on('data', function(d) {
            resString += d;
        });
        res.on('end', function() {
            response.send(resString);
        });
    });
    req.write(JSON.stringify(request.body));
    req.end();
});

team.html

<div ng-repeat="team in teamData">
    <h2>{{team.name}}</h2>
    ....
</div>

我的目标是在PUT请求发生后更新ng-repeat列表(无页面刷新)。我怎样才能实现它?

1 个答案:

答案 0 :(得分:2)

将其分配回模型。例如,如果你的ng-repeat在$ scope.item上,那么:

 return $http(req).then(function(res){
        callback(res);
        $scope.item = res
      }, function(err){
        callback(err);
      });