Html 将textarea内容绑定到md卡时保留换行符

Html 将textarea内容绑定到md卡时保留换行符,html,angularjs,angular-material,Html,Angularjs,Angular Material,我有一个文本区和一张md卡 angular.module('dashboard',['ngMaterial'])) .config(函数($mdThemingProvider){ $mdThemingProvider.theme('default') .dark() .primaryPalette(“靛蓝”) .accentPalette(“黄色”); }) .controller('previewCtrl',function(){ //previewCtrl=这个; this.preview

我有一个文本区和一张md卡

angular.module('dashboard',['ngMaterial']))
.config(函数($mdThemingProvider){
$mdThemingProvider.theme('default')
.dark()
.primaryPalette(“靛蓝”)
.accentPalette(“黄色”);
})
.controller('previewCtrl',function(){
//previewCtrl=这个;
this.previewData=previewData;
});

标题
副标题

要通过AngularJS将HTML注入,可以使用
ng bind HTML
指令而不是
ng bind

它要求您将
ngSanitize
模块插入应用程序,并首先导入其JS文件

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-sanitize.min.js"></script>
现在您可以使用
ng bind html
指令和
newLines
过滤器,如下所示:

app.filter('newLines', function () {
    return function(text) {
        if(text)
            return text.replace(/\\n/g, '<br/>');
    };
});
<md-card-content ng-bind-html="form.text | newLines">
</md-card-content>


以下是应用上述修改后的代码:

angular.module('dashboard',['ngMaterial','ngSanitize']))
.config(函数($mdThemingProvider){
$mdThemingProvider.theme('default')
.dark()
.primaryPalette(“靛蓝”)
.accentPalette(“黄色”);
})
.controller('previewCtrl',function(){
//previewCtrl=这个;
this.previewData=previewData;
})
.filter('换行符',函数(){
返回函数(文本){
如果(文本)
返回文本。替换(/\\n/g,“
”); }; });

标题
副标题

您可以尝试改用
ng bind html
属性吗?