AngularJS将<br>呈现为文本而不是换行符

Lan*_*nce 47 javascript angularjs

我确信以前曾经问过,但我找不到答案.

我有一个AngularJS脚本,它从数据库中提取,然后渲染到内容.一切都正常工作,除了几个地方,我试图将一些单词与它们之间的新行连接起来.

 **in the script.js**
groupedList[aIndex].CATEGORY = existingCategory+'\n'+thisCategory;
groupedList[aIndex].CATEGORY = existingCategory+'<br>'+thisCategory;
Run Code Online (Sandbox Code Playgroud)

如果我使用上面代码的第一行,我没有看到任何内容,但在redered html中没有新行.如果我使用第二行,则<br>呈现为文本,输出如下所示:

Instinct<br>Media
Run Code Online (Sandbox Code Playgroud)

代替

Instinct
Media
Run Code Online (Sandbox Code Playgroud)

我可以发布完整的脚本,如果这将有所帮助,但我猜我有一些简单的东西,我只是没有看到.

更新 这里是完整的js

function qCtrl($scope, $filter, $http, $timeout){

    $scope.getCategories = function(){$http.post('quote.cfc?method=getCategories').success(function(data) { $scope.categories = data; });   }
    $scope.getClassifications = function(){$http.post('quote.cfc?method=getClassifications').success(function(data) {   $scope.classifications = data;  }); }
    $scope.getIndustries = function(){$http.post('quote.cfc?method=getIndustries').success(function(data) { $scope.industries = data;   }); }
    $scope.getKeywords = function(){$http.post('quote.cfc?method=getKeywords').success(function(data) { $scope.keywords = data; }); }
    $scope.getSources = function(){$http.post('quote.cfc?method=getSources').success(function(data) {   $scope.sources = data;  }); }

    $scope.getAllQuotes = function(){$http.post('quote.cfc?method=getAllQuotesJoined').success(function(data) { $scope.allQuotes = data;    }); }

    $scope.initScopes = function (){
        $scope.getCategories();
        $scope.getClassifications();
        $scope.getIndustries();
        $scope.getKeywords();
        $scope.getSources();
        $scope.getAllQuotes();
    }   
    $scope.initScopes();

    // SEARCH QUOTES
    $scope.filteredQuotes = $scope.allQuotes;
    $scope.search = {searchField:''};

    $scope.searchQuote = function(){
        var filter = $filter('filter');
        var searchCrit = $scope.search;
        var newlist = $scope.allQuotes;
        var groupedList = [];
        var idList = [];
        newlist = filter(newlist,{TESTQUOTE:searchCrit.searchField});
        for(i=0;i<10;i++){
            aIndex = idList.contains(newlist[i].TESTIMONIALID);
            if(aIndex >= 0){
                thisKeyword = newlist[i].KEYWORD;
                thisClassification = newlist[i].CLASSIFICATION;
                thisCategory = newlist[i].CATEGORY;
                existingKeyword = groupedList[aIndex].KEYWORD;
                existingClassification = groupedList[aIndex].CLASSIFICATION;
                existingCategory = groupedList[aIndex].CATEGORY;
                if(thisKeyword != '' && existingKeyword.indexOf(thisKeyword) == -1){
                    groupedList[aIndex].KEYWORD = existingKeyword+' - '+thisKeyword;
                } 
                if(thisClassification != '' && existingClassification.indexOf(thisClassification) == -1){
                    groupedList[aIndex].CLASSIFICATION = existingClassification+' \n '+thisClassification;
                } 
                if(thisCategory != '' && existingCategory.indexOf(thisCategory) == -1){
                    groupedList[aIndex].CATEGORY = existingCategory+'<br>'+thisCategory;
                }               
            } else {
                idList.push(newlist[i].TESTIMONIALID);
                groupedList.push(newlist[i]);
            }
        }
        $scope.filteredQuotes = groupedList;
      }
}
Array.prototype.contains = function ( needle ) {
   for (j in this) {
       if (this[j] == needle) return j;
   }
   return -1;
}
Run Code Online (Sandbox Code Playgroud)

这是HTML

<div ng-repeat="q in filteredQuotes" class="well clearfix">
                        <h3>{{q.TITLE}}</h3>
                        <div class="row-fluid" style="margin-bottom:5px;">
                            <div class="span3 well-small whBG"><h4>Classification</h4>{{q.CLASSIFICATION}}</div>
                            <div class="span3 well-small whBG pipeHolder"><h4>Categories</h4>{{q.CATEGORY}}</div>
                            <div class="span3 well-small whBG"><h4>Key Words</h4>{{q.KEYWORD}}</div>
                            <div class="span3 well-small whBG"><h4>Additional</h4>Industry = {{q.INDUSTRY}}<br>Source = {{q.SOURCE}}</div>
                        </div>
                        <div class="well whBG">{{q.TESTQUOTE}}</div>
                        <div class="tiny">
                            Source comment : {{q.SOURCECOMMENT}}<br>
                            Additional Comment : {{q.COMMENT}}
                        </div>
                    </div>
                </div>
Run Code Online (Sandbox Code Playgroud)

vin*_*esh 54

您可以使用\n连接单词,然后将此样式应用于容器div.

style="white-space: pre;"
Run Code Online (Sandbox Code Playgroud)

有关详细信息,访问https://developer.mozilla.org/en-US/docs/Web/CSS/white-space

<p style="white-space: pre;">
    This is normal text.
</p>
<p style="white-space: pre;">
    This 
  text 
  contains 
  new lines.
</p>
Run Code Online (Sandbox Code Playgroud)

  • 我喜欢这个答案!应该是接受的答案! (8认同)
  • 很好 我使用`white-space:pre-wrap;`来包裹文本 (2认同)

Shm*_*dty 41

我可能是错的,因为我从未使用过Angular,但我相信你可能正在使用ng-bind,这将只创建一个TextNode.

你会想要使用ng-bind-html.

http://docs.angularjs.org/api/ngSanitize.directive:ngBindHtml

更新:看起来你需要使用ng-bind-html-unsafe='q.category'

http://docs.angularjs.org/api/ng.directive:ngBindHtmlUnsafe

这是一个演示:

http://jsfiddle.net/VFVMv/


Ben*_*esh 25

您需要使用ng-bind-html-unsafe...或者您需要包含ngSanitize模块并使用ng-bind-html:

使用ng-bind-html-unsafe

如果您信任正在呈现的HTML源,则使用此选项将呈现您放入其中的任何内容的原始输出.

<div><h4>Categories</h4><span ng-bind-html-unsafe="q.CATEGORY"></span></div>
Run Code Online (Sandbox Code Playgroud)

或者使用ng-bind-html

如果您不信任HTML的来源(即它的用户输入),请使用此选项.它将清理html,以确保它不包含脚本标记或其他潜在安全风险来源.

确保包含以下内容:

<script src="http://code.angularjs.org/1.0.4/angular-sanitize.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

然后在您的应用程序模块中引用它:

var app = angular.module('myApp', ['ngSanitize']);
Run Code Online (Sandbox Code Playgroud)

然后使用它:

<div><h4>Categories</h4><span ng-bind-html="q.CATEGORY"></span></div>
Run Code Online (Sandbox Code Playgroud)

  • +1我希望有人真正知道Angular会出现.哈哈. (4认同)