在与angularjs绑定的同时,将字符串断开。

时间:2021-09-06 22:16:33

i have problem in giving line break in string while binding the data in angularJS

在用angularJS绑定数据时,在字符串中给出换行符是有问题的

<h3  ng-bind="thirdContainHeaderOneTitle"></h3> 
$scope.thirdContainHeaderOneTitle = "my + '<br>' + hdjsd";

the <br>,'<br>',' \/n' are not working and i dont know why..



',' \/n'不工作,我不知道为什么…

3 个解决方案

#1


3  

See https://docs.angularjs.org/api/ng/directive/ngBindHtml along with the posted example:

请参阅https://docs.angularjs.org/api/ng/directive/ngBindHtml以及已发布的示例:

View:

观点:

<div ng-controller="ngBindHtmlCtrl">
 <p ng-bind-html="myHTML"></p>
</div>

Application:

应用程序:

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>';
}]);

Note that you need to include the ngSanitize module.

注意,您需要包含ngSanitize模块。

#2


0  

You can't just bind html in your strings. Try something like this instead.

不能在字符串中绑定html。试试这个。

<div ng-bind-html-unsafe="thirdContainHeaderOneTitle"></div>

That should parse out your HTMl and give the effect you want.

这将解析HTMl并给出您想要的效果。

#3


0  

You cannot sent any html code through the $scope

不能通过$范围发送任何html代码

Its better you use a directive and inside of that , there will be a template, then you can send any html you want like this :

你最好使用一个指令,里面有一个模板,然后你可以像这样发送任何你想要的html:

  app.directive('yourdirective',function(){
      return {
        restrict:"E",
        template:"my" + "<br>" + "hdjsd"
        } 
   });

and in your html :

在html中:

 <yourdirective></yourdirective>

#1


3  

See https://docs.angularjs.org/api/ng/directive/ngBindHtml along with the posted example:

请参阅https://docs.angularjs.org/api/ng/directive/ngBindHtml以及已发布的示例:

View:

观点:

<div ng-controller="ngBindHtmlCtrl">
 <p ng-bind-html="myHTML"></p>
</div>

Application:

应用程序:

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>';
}]);

Note that you need to include the ngSanitize module.

注意,您需要包含ngSanitize模块。

#2


0  

You can't just bind html in your strings. Try something like this instead.

不能在字符串中绑定html。试试这个。

<div ng-bind-html-unsafe="thirdContainHeaderOneTitle"></div>

That should parse out your HTMl and give the effect you want.

这将解析HTMl并给出您想要的效果。

#3


0  

You cannot sent any html code through the $scope

不能通过$范围发送任何html代码

Its better you use a directive and inside of that , there will be a template, then you can send any html you want like this :

你最好使用一个指令,里面有一个模板,然后你可以像这样发送任何你想要的html:

  app.directive('yourdirective',function(){
      return {
        restrict:"E",
        template:"my" + "<br>" + "hdjsd"
        } 
   });

and in your html :

在html中:

 <yourdirective></yourdirective>