我有jsp页面,其中我用参数调用一个angularjs函数。该函数使得获取http请求并将结果存储到控制器中的变量。但是当我从jsp页面调用该函数时,我得到的情况是这些http请求被无限地一遍又一遍地发送。如何停止该循环并只调用一次函数?
在jsp中我有:
{{ getFriends('john') }} //call to angularjs function to retrieve all of John's friends
在js:
$scope.friends = null;
$scope.getFriends = function(username){
$http.get("services/rest/getFriends?username="+username).then(function(response){
$scope.friends = response.data;
console.info(response.data);
}, function(response){
});
}
我收到了这个错误:
angular.min.js:sourcemap:123 Error: [$rootScope:infdig] http://errors.angularjs.org/1.6.3/$rootScope/infdig?p0=10&p1=%5B%5D
at angular.min.js:sourcemap:6
at m.$digest (angular.min.js:sourcemap:147)
at m.$apply (angular.min.js:sourcemap:149)
at l (angular.min.js:sourcemap:102)
at XMLHttpRequest.v.onload (angular.min.js:sourcemap:107)
jsp的完整代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html ng-app = "profil">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script src="${pageContext.request.contextPath}/js/angular.min.js"></script>
<script src="${pageContext.request.contextPath}/js/profil.js"></script>
<title>Insert title here</title>
</head>
<body ng-controller="appController">
<!-- {{ getFriends('pera') }} -->
{{ getFriends('${userDetails.username }') }}
Ime: ${userDetails.ime } </br>
Prezime: ${userDetails.prezime } </br>
Korisnicko ime: ${userDetails.username } </br>
<img src="${pageContext.request.contextPath}/slike/${userDetails.slika}" alt="${userDetails.username }" height="50px"> </img>
</body>
</html>
和控制器:
@RequestMapping("user-details/{username}")
public String showUserDetails(@PathVariable String username, Model model) throws SQLException {
model.addAttribute("userDetails",dbHelper.getKorisnik(username));
return "/profil.jsp";
}
其他控制器:
@RequestMapping(path="getPrijatelji",method = {RequestMethod.GET} , produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public List<Korisnik> getPrijatelji(@RequestParam(name = "username") String username) throws SQLException{
return dbHelper.getPrijatelji(username);
}
答案 0 :(得分:0)
你可以在ng-init中调用你的函数,而不是调用占位符或ng-bind。