繁体   English   中英

AngularJS 正在渲染<br>作为文本而不是换行符

[英]AngularJS is rendering <br> as text not as a newline

我确信以前有人问过这个问题,但我找不到答案。

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

 **in the script.js**
groupedList[aIndex].CATEGORY = existingCategory+'\n'+thisCategory;
groupedList[aIndex].CATEGORY = existingCategory+'<br>'+thisCategory;

如果我使用上面代码的第一行,我什么也看不到,但在重新编写的 html 中没有新行。 如果我使用第二行, <br>将呈现为文本,输出如下所示:

Instinct<br>Media

代替

Instinct
Media

如果有帮助,我可以发布完整的脚本,但我猜有一些我没有看到的简单内容。

更新这是完整的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;
}

这是 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>

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

style="white-space: pre;"

有关详细信息, 访问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> 

我可能是错的,因为我从未使用过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/

您需要使用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>

或者使用ng-bind-html

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

确保包含以下内容:

<script src="http://code.angularjs.org/1.0.4/angular-sanitize.min.js"></script>

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

var app = angular.module('myApp', ['ngSanitize']);

然后使用它:

<div><h4>Categories</h4><span ng-bind-html="q.CATEGORY"></span></div>

我这样用过

function chatSearchCtrl($scope, $http,$sce) {
 // some more my code

// take this 
data['message'] = $sce.trustAsHtml(data['message']);

$scope.searchresults = data;

并在HTML中我做了

<p class="clsPyType clsChatBoxPadding" ng-bind-html="searchresults.message"></p>

这就是它让我的<br/>标签渲染

为什么这么复杂?

我这样简单地解决了我的问题:

  <pre>{{existingCategory+thisCategory}}</pre>

如果字符串包含'\\ n',当我从textarea保存数据时,它会自动生成<br />

您还可以使用:

String.fromCharCode(10);

使用 CSS

white-space: pre-line;

这里是工作示例: https : //jsfiddle.net/Nxja/3xtcqdej/1/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM