使用自定义read-more指令进行ng-repeat

时间:2015-11-11 01:27:51

标签: javascript angularjs angularjs-ng-repeat ng-repeat angular-directive

我有一个ng-repeat,ng-repeat="post in posts track by $index",然后是带有自定义指令的'p'标签,该指令会限制帖子中的字词数量,直到用户点击“了解更多”为止。

<p read-more>{{post.thePost}}</p>

我收到错误:

  

TypeError:无法读取未定义的属性“split”。

当我在'p'标签之间有纯文本时,它会有效,但当我有$scope.posts的多个项目时,我认为它无法读取实际文本。

有什么方法可以解决这个问题吗?

这是我正在使用的read-more角度指令:

https://github.com/pattysnippets/angular-readmore

1 个答案:

答案 0 :(得分:2)

密钥确实使用了content属性。

以下是关键标记:

<p ng-show="loading">Loading posts...</p>
<p ng-repeat="post in posts track by $index" read-more content="{{post.text}}"></p>

这是我的控制器代码,模拟从服务器发布的帖子:

app.controller('MainCtrl', function($scope, $timeout) {
  $scope.loading = true;
  $timeout(function() {
    $scope.loading = false;
    $scope.posts = [1, 2, 3].map(function(id) {
      return {
        id: id,
        heading: 'Post ' + id,
        text: 'Lorem ipsum dolor (...etc...)'
      };
    });
  }, 1000);
});

这是the Plunkr with it working

相关问题