AngularJS: newline characters to paragraph elements

后端 未结 4 1722
遥遥无期
遥遥无期 2020-12-31 21:17

Within Angular, I need to generate a series of paragraph elements from a block of text containing newline characters?

I can think of a couple of ways to do this. Ho

4条回答
  •  隐瞒了意图╮
    2020-12-31 22:13

    The best solution I could think of was to create a filter:

    angular.module('myApp').
    filter('nlToArray', function() {
      return function(text) {
          return text.split('\n');
      };
    });
    

    This takes a block of text and creates a new array element for each paragraph.

    This array can then be plugged into an ng-repeat directive:

    {{paragraph}}

提交回复
热议问题