Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在每次使用angularjs调用函数时附加web服务json数据_Javascript_Angularjs_Json_Html_Web Services - Fatal编程技术网

Javascript 如何在每次使用angularjs调用函数时附加web服务json数据

Javascript 如何在每次使用angularjs调用函数时附加web服务json数据,javascript,angularjs,json,html,web-services,Javascript,Angularjs,Json,Html,Web Services,在这里,我每秒都调用函数getData,除了追加之外,一切都很顺利。 每次函数调用angularjs时,都会将旧数据替换为新数据。 但我想在现有数据之后追加。任何人都可以帮忙 <div ng-app="serviceConsumer"> <div ng-controller="questionsController"> <div ng-repeat="j in LiveChat"> <div> <d

在这里,我每秒都调用函数getData,除了追加之外,一切都很顺利。 每次函数调用angularjs时,都会将旧数据替换为新数据。 但我想在现有数据之后追加。任何人都可以帮忙

     <div ng-app="serviceConsumer">
     <div ng-controller="questionsController">
    <div ng-repeat="j in LiveChat">
    <div>
    <div>{{j.UserName2}}</div>
    <div>
    <div>{{j.Text2}}<div>
    </div>
    </div>
    </div>
    </div>
    </div>
    </div>
    </div>
    <script>
    var app = angular.module('serviceConsumer', []);
    app.controller('questionsController', function ($scope, $http) {
    $scope.getData = function () {
            $http({
                method: "GET",
                url: "/GetUserDataWebServices.asmx/GetLiveChatListData",
                params: {
                    username: $('.ExeCutiveName').text()
                }
            })
             .success(function (data) {
                 var myjson = JSON.parse(data);
                 $scope.LiveChat = JSON.parse(myjson);
                 //here i want to append myjson to existing ng-repeat <div>
             });
        }
    )};
    setInterval($scope.getData, 1000);
    </script>

{{j.UserName2}
{{j.Text2}
var app=angular.module('serviceConsumer',[]);
app.controller('questionsController',函数($scope,$http){
$scope.getData=函数(){
$http({
方法:“获取”,
url:“/GetUserDataWebServices.asmx/GetLiveChatListData”,
参数:{
用户名:$('.ExeCutiveName').text()
}
})
.成功(功能(数据){
var myjson=JSON.parse(数据);
$scope.LiveChat=JSON.parse(myjson);
//这里我想将myjson附加到现有的ng repeat
});
}
)};
setInterval($scope.getData,1000);

您需要使用
push
方法


比如
$scope.LiveChat.push(JSON.parse(myjson))

将concat数据重新分配给它

像这样

$scope.LiveChat = $scope.LiveChat.concat(JSON.parse(myjson));

op正在获取列表而不是单个对象。如果你把列表推到另一个列表中,它会把推列表推到一个索引中,而不是合并它。嗨..Anik这里我必须使用两个函数,一个用于页面加载,一个用于setInterval()。有没有办法在同一函数中同时使用“$scope.LiveChat=JSON.parse(myjson);”和“$scope.LiveChat=$scope.LiveChat.concat(JSON.parse(myjson));”是的,您可以。。只需检查LiveChat中是否有数据。。像这样
if($scope.LiveChat.length>0)$scope.LiveChat=$scope.LiveChat.concat(JSON.parse(myjson));else$scope.LiveChat=JSON.parse(myjson)或者您可以像这样使用三元值
$scope.LiveChat=$scope.LiveChat.length>0$scope.LiveChat.concat(JSON.parse(myjson)):JSON.parse(myjson)@Nitesh