Javascript angularjs$http请求被多次调用
我向servlet的doGet方法发出简单的http请求,以从表中获取数据。下面是我的密码Javascript angularjs$http请求被多次调用,javascript,angularjs,servlets,Javascript,Angularjs,Servlets,我向servlet的doGet方法发出简单的http请求,以从表中获取数据。下面是我的密码 var myApp = angular.module('myApp', []); myApp.controller('myController', function ($scope,$http) { $scope.getDataFromServer = function() { $http ({ method:'GET', url
var myApp = angular.module('myApp', []);
myApp.controller('myController', function ($scope,$http) {
$scope.getDataFromServer = function() {
$http
({
method:'GET',
url:'jasoncontroller'
}).success(function(data, status, headers, config){
$scope.sqldata = data;
console.log("executing");
$scope.$watch('sqldata',function(newValue,oldValue){
console.info('changed');
console.log('new : ' + newValue);
console.log('old : ' + oldValue);
console.log('watch end');
});
}).error(function(data, status, headers, config){});
};
}))
servlet-doGet方法
每当我单击获取数据时,一旦我在tabname=“saleq315”中更改了表名,它就不会获取新数据;多次单击后,表数据刷新。
当我将此记录到控制台时,我看到http请求被多次执行。
如果您看到附加的图像,我更改了表名并单击以获取数据
first click - same table data --> console log one entry
seconds click - same table data --> console log one entry
third click - got new data --> console log 5 entries.
Not sure what is happening here.
[![enter image description here][1]][1]
在文档中:“每次调用$digest()时都会调用watchExpression,并应返回将被监视的值。(watchExpression在使用同一输入执行多次时不应更改其值,因为$digest()可能会多次执行它)。也就是说,watchExpression应该是幂等的。这意味着您在做某事之前必须检查
oldValue
和newValue
的相等性。顺便说一句,我希望您不要多次调用getDataFromServer
,因为您将创建多个观察程序。您应该只创建一次$watch,并且在callb之外http呼叫确认。
first click - same table data --> console log one entry
seconds click - same table data --> console log one entry
third click - got new data --> console log 5 entries.
Not sure what is happening here.
[![enter image description here][1]][1]