简体   繁体   English

ng-repeat与自定义read-more指令不起作用

[英]ng-repeat with a custom read-more directive not working

I have an ng-repeat, ng-repeat="post in posts track by $index" , then a 'p' tag with a custom directive that will limit the amount of words in the post until the user clicks 'read more'. 我有一个ng-repeat, ng-repeat="post in posts track by $index" ,然后是带有自定义指令的'p'标签,它会限制帖子中的单词数量,直到用户点击“阅读更多”为止。

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

I am getting the error: 我收到错误:

TypeError: Cannot read property 'split' of undefined. TypeError:无法读取未定义的属性“split”。

It will work when I have plain text in between the 'p' tags, but when I have multiple items from $scope.posts , I think it is failing to read the actual text. 当我在'p'标签之间有纯文本时它会起作用,但是当我有来自$scope.posts多个项目时,我认为它无法读取实际文本。

Any way to get around this? 有办法解决这个问题吗?

Here is the read-more angular directive I am using: 这是我正在使用的read-more角度指令:

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

The key does seem to be using the content attribute. 密钥似乎确实使用了content属性。

Here is the key markup: 这是关键标记:

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

And here is my controller code that simulates loading in posts from the server: 这是我的控制器代码,模拟从服务器的帖子中加载:

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

Here is the Plunkr with it working . 这是Plunkr与它一起工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM