javascript - Angular ng-repeat <li> 字符串换行

标签 javascript css angularjs

  • 查看:

    <li ng-repeat='msg in msgs'>{{msg}}</li>
    

  • Controller :

    msgs=['abc','a
                 b
                 v', '123']
    
    msgs.push('Something input from textarea, maybe has line feed')
    

  • 想要:

    • abc
    • a
      b
      v
    • 123

  • 但是结果换行消失了

    • abc
    • a b v
    • 123

  • 尝试解决

    try refactoring < li> label to <myli>
    
    app.directive('myli', function ($rootScope) {
        return {
            restrict: 'E',
            replace: true,
            template: "<li></li>",
            link: function(scope, element, attr) {
                element.append(scope.msg);
            }
        }
    });
    

    但这不是我想要的,它只是改变初始数据。

    如果我用换行符将某些内容推送到消息,换行符将更改为空格。

    帮帮我

  • 最佳答案

    使用 CSS white-space属性...

    angular.module('so', []).run(function($rootScope) {
      $rootScope.msgs = ['abc', `a
                 b
                 v`, '123'];
    
      $rootScope.msgs.push('Something input from textarea, maybe has line feed')
    });
    li {
      white-space: pre-line;
    }
    <ul ng-app="so">
      <li ng-repeat="msg in msgs">{{msg}}</li>
    </ul>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>

    关于javascript - Angular ng-repeat <li> 字符串换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39969515/

    相关文章:

    javascript - 更改JointJs Paper的交互属性?

    angularjs - 在状态名称中使用点表示法时,Angular + UI Router 会中断

    css - 均匀间隔单词,除非在多词短语中(如 'human rights' )

    html - 无法弄清楚如何修复 CSS 宽度

    html - 使用angularjs和bootstrap在响应式div中固定位置div

    node.js - Express Session + Angular : can't access connect. sid cookie

    javascript - Angular SC.get 中的 Soundcloud API 不暂停

    javascript - 表中的列默认排序

    javascript - 将 Typeahead.js Bloodhound 与 Browserify 一起使用会给出空对象 {}

    javascript - 相当于jquery中的onclick ="update_x(this)"? (调用函数)