String 使用angularjs绑定字符串时,字符串中的换行符
在绑定angularJS中的数据时,我在字符串中给出换行符时遇到问题String 使用angularjs绑定字符串时,字符串中的换行符,string,angularjs,line,break,String,Angularjs,Line,Break,在绑定angularJS中的数据时,我在字符串中给出换行符时遇到问题 <h3 ng-bind="thirdContainHeaderOneTitle"></h3> $scope.thirdContainHeaderOneTitle = "my + '<br>' + hdjsd"; $scope.thirdContainHeadRoneTitle=“my+”“+hdjsd”; ,“”,“\/n”不起作用,我不知道为什么..您不能只在字符串中绑定html
<h3 ng-bind="thirdContainHeaderOneTitle"></h3>
$scope.thirdContainHeaderOneTitle = "my + '<br>' + hdjsd";
$scope.thirdContainHeadRoneTitle=“my+”
“+hdjsd”;
,“
”,“\/n”
不起作用,我不知道为什么..您不能只在字符串中绑定html。试试这样吧
<div ng-bind-html-unsafe="thirdContainHeaderOneTitle"></div>
这将解析出您的HTMl并提供您想要的效果。您不能通过$scope发送任何HTMl代码 最好使用一个指令,其中会有一个模板,然后您可以像这样发送任何html:
app.directive('yourdirective',function(){
return {
restrict:"E",
template:"my" + "<br>" + "hdjsd"
}
});
app.directive('yourdirection',function(){
返回{
限制:“E”,
模板:“我的”+“
”+“hdjsd”
}
});
在您的html中:
<yourdirective></yourdirective>
请参见以下内容:
视图:
请注意,您需要包括
<div ng-controller="ngBindHtmlCtrl">
<p ng-bind-html="myHTML"></p>
</div>
angular.module('ngBindHtmlExample', ['ngSanitize'])
.controller('ngBindHtmlCtrl', ['$scope', function ngBindHtmlCtrl($scope) {
$scope.myHTML =
'I am an <code>HTML</code>string with <a href="#">links!</a> and other <em>stuff</em>';
}]);