$sce 서비스, 서버에서 전송된 데이터를 렌더링 가능한 html 데이터로 설정

3558 단어 angularjs
서버 측:
서비스 계층:
@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    private SolrTemplate solrTemplate;

    /**
     *
     * @param param
     * @return
     */
    @Override
    public Map search(Map param) {
        Map data = new HashMap<>();
        Query query = new SimpleQuery("*:*");
        String keywords = (String) param.get("keywords");
        if (keywords != null && StringUtils.isNotBlank(keywords)){
            HighlightOptions options = new HighlightOptions();
            options.addField("title");
            options.setSimplePrefix("");
            options.setSimplePostfix("");
            query = new SimpleHighlightQuery().setHighlightOptions(options);
            Criteria criteria = new Criteria("keywords").is(keywords);
            query.addCriteria(criteria);
            HighlightPage solrItems = solrTemplate.queryForHighlightPage((HighlightQuery) query, SolrItem.class);
            List> highlighted = solrItems.getHighlighted();
            for (HighlightEntry entry : highlighted) {
                SolrItem entity = entry.getEntity();
                if ( entry.getHighlights().size()> 0 && entry.getHighlights().get(0).getSnipplets().size()> 0){
                    String title = entry.getHighlights().get(0).getSnipplets().get(0);
                    entity.setTitle(title);
                }
            }
            List content = solrItems.getContent();
            data.put("rows",content);
            return data;
        }
        ScoredPage solrItems = solrTemplate.queryForPage(query, SolrItem.class);
        List content = solrItems.getContent();
        data.put("data",content);
        return data;
    }
}

Controller 레이어:
@RestController
@RequestMapping("/search")
public class SearchController {

    @Reference(timeout = 10000)
    private SearchService searchService;

    @PostMapping("/searchItem")
    public Map searchItem(@RequestBody Map param){
        try {
            return searchService.search(param);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

js 코드:

var app=angular.module("myApp",[]);
app.controller("mycontroller",function ($scope, $sce, $http) {
$scope.trustHtml = function(data) {
    return $sce.trustAs($sce.HTML, data);
};</code></pre> 
  <pre><code class="language-html hljs">$scope.searchEntity = {};
$scope.search=function () {
    $http.sendPost("/search/searchItem",$scope.searchEntity).then(
        function (response) {
            $scope.map = response.data;
        }
    );
});

html :




HelloWord




ng-bind-html textarea

 

좋은 웹페이지 즐겨찾기