2016-09-26 8 views
0

ユーザー入力によって生成されたページに単語のリストを表示するために使用したいHipsterアプリケーションがあります。例:ユーザーが文字列 'ba'を入力すると、アプリケーションは 'ba'で始まるローカルデータ構造内にあるすべての単語のリストを生成します。私が理解しようとしているのは、文字列をサーバーに戻してリストを返す方法です。それは$scope変数のためですか?残りのエンドポイントからのオブジェクトのリストを表示するAngularJS?

@RestController 
@RequestMapping("/api") 
public class WordResource { 

private final Logger log = LoggerFactory.getLogger(WordResource.class); 

@Inject 
private TreeService treeService; 

/** 
* GET /words : get all the words. 
* 
* @return the ResponseEntity with status 200 (OK) and the list of words in body 
*/ 
@RequestMapping(value = "/words", 
    method = RequestMethod.GET, 
    produces = MediaType.APPLICATION_JSON_VALUE) 
@Timed 
public List<Word> getAllWords() { 
    log.debug("REST request to get all Words"); 
    return treeService.getAllWords(); 
} 

/** 
* GET /words : get all the words. 
* 
* @return the ResponseEntity with status 200 (OK) and the list of words in body 
*/ 
@RequestMapping(value = "/ten-words", 
    method = RequestMethod.GET, 
    produces = MediaType.APPLICATION_JSON_VALUE) 
@Timed 
public List<Word> getSubWords(@PathVariable String word) { 
    log.debug("REST request to get all Words"); 
    return treeService.getTenWordsFrom(word); 

} 
} 

HTML

<div ng-cloak> 
<!DOCTYPE html> 
<html lang="en" 
     xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:th="http://www.thymeleaf.org"> 

<head> 
    <meta charset="UTF-8"/> 
    <!--/*@thymesVar id="pageTitle" type=""*/--> 
    <title th:text="${pageTitle}">title placeholder</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1"/> 

    <link rel="stylesheet" href="https://bootswatch.com/superhero/bootstrap.min.css"/> 

    <!-- Polyfill(s) for older browsers --> 
    <script src="node_modules/core-js/client/shim.min.js"></script> 

</head> 

<body> 

<form> 
    <div class="form-group"> 
     <input type="text" name="searchStr" class="form-control" placeholder="Search Words..."/> 
    </div> 
</form> 


<div class="row well"> 
    <div class="col-md-4"> 

     <!--word list goes here--> 

    </div> 
</div> 

</body> 
</html> 

</div> 

コントローラー:

(function() { 
'use strict'; 

angular 
    .module('treeWordsApp') 
    .controller('HomeController', HomeController); 

HomeController.$inject = ['$scope', 'Principal', 'LoginService', '$state']; 

function HomeController ($scope, Principal, LoginService, $state) { 
    var vm = this; 

    vm.account = null; 
    vm.isAuthenticated = null; 
    vm.login = LoginService.open; 
    vm.register = register; 

    $scope ({ 


    }); 

} 
})(); 
私は私が把握しようとしていることは...

RestControllerを結合2ウェイデータを実装する方法だと思います

答えて

2

私はコンセプトを少し混ぜたと思う。 $ scopeは、クライアントレベルで双方向データバインディングを作成するために使用されます。つまり、クライアントでローカルに変更されるすべてのものがUIで更新され、その逆も同様です。

ローカルバインドを作成する前に、クライアント(HomeController)は、あなたの場合は「WordResource」サービスのどこかからデータを取得する必要があります。これを行うには、AngularJSの "$ http"サービスを使用できます。 https://docs.angularjs.org/api/ng/service/$http

$ httpサービスはJavaサービスを呼び出し、必要なすべてのデータをコントローラに戻します。

$scope.wordList = []; 

をそして、2つの方法でバインドを作成:何、クライアント側で行うために残されていることのが「ワードリスト」CALせ、単語のリストを保持するためにローカル$スコープ内の変数を作成することですngRepeatは、例えば、リストの各要素に対して指定されたHTML要素を作成しますhttps://docs.angularjs.org/api/ng/directive/ngRepeat

::、例えば、「NGリピート」を使用してインターフェイス、

<div ng-repeat="word in wordList"> 
    <span>{{word}}</span> 
</div> 

は... DIVを作成します。リスト内の単語ごとに子SPANが表示されます。

これは双方向バインドを作成しますが、つまり、$ scope内のリストの変更がUIに伝播され、UIの変更が$ scopeに反映されるということです。サーバー内の何かを変更したい場合は、この質問の範囲を超えたAPI呼び出しとリソースの更新のために、いくつかの余分なロジックを追加する必要があります。要約すると

  1. UI
  2. にあなたが単語のリストを更新するたびに結合HTMLを作成した単語
  3. のリストを保持するためにローカル$スコープで変数を作成します。 $ httpを使用してAPI呼び出しを行い、$ scope.wordListにサービスから得た単語のリストを入力してください。

これは役に立ちます。

関連する問題