2017-07-22 19 views
0

パラメータを持つangularjs関数を1つ呼び出すJSPページがあります。この関数は、httpリクエストを取得し、結果をコントローラに格納します。しかし、jspページからその関数を呼び出すと、そのhttpリクエストが無限に何度も何度も送信される状況が発生します。どのようにループを停止し、関数呼び出しを1回だけ行うのですか? JSPで無限ループangularjs

私が持っている:JSで

{{ getFriends('john') }} //call to angularjs function to retrieve all of John's friends 

:JSPの

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) 

全コード:

<%@ 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> 
$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){ 

      }); 

     } 

が、私はこのエラーを取得します

コントローラ:

@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

API呼び出しの.then内に何かを返すようにしてください。 – Vivz

+0

@Vivz Nothing、still loops。 – wdc

+0

あなたのJSPページに何かレンダリングがありますか?コンソールに何かエラーがありますか? – Vivz

答えて

0

プレースホルダまたはNG-バインドに呼び出す代わりに、NG-initの中であなたの関数を呼び出すことができます。