首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用控制器更改ng-bind-html值

使用控制器更改ng-bind-html值
EN

Stack Overflow用户
提问于 2016-04-10 12:16:58
回答 2查看 1.1K关注 0票数 0

我想要更改ng-bind,但是不知道怎么做。

我最初的ng-bind-html vale是:-

代码语言:javascript
复制
<p class="forum-content" id="{{post.id}}" ng-bind-html="renderHtml(post.content)|truncate:90"></p>

并想要这个价值:

<p class="forum-content" id="{{post.id}}" ng-bind-html="renderHtml(post.content)"></p>

在按钮被按下之后。

最初,它将我的数据截断为90个字符,我希望在按下按钮后显示完整的数据。我的主要动机是显示完整的数据后,按钮(读-更多)被按.

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-10 12:34:14

简单的技巧:

代码语言:javascript
复制
<p class="forum-content" id="{{post.id}}" ng-bind-html="renderHtml(post.content)|truncate:limit"></p>

<button ng-click="showAll()">Show all</button>

在主计长中:

代码语言:javascript
复制
$scope.limit = 90;
$scope.showAll = function() {
    $scope.limit = 100000000; // should be large enough not to truncate anything
};

尽管如此,这仍然会试图截断。如果您真的想避免它,一个更干净的解决方案是在控制器中使用过滤器:

代码语言:javascript
复制
<p class="forum-content" id="{{post.id}}" ng-bind-html="renderAndTruncateHtml(post.content)"></p>

<button ng-click="showAll()">Show all</button>

在主计长中:

代码语言:javascript
复制
$scope.shouldTruncate = true;
$scope.renderAndTruncateHtml = function(value) {
    var result = $scope.renderHtml(value);
    if ($scope.shouldTruncate) {
        result = $filter('truncate')(result, 90);
    }
    return result;
};
$scope.showAll = function() {
    $scope.shouldTruncate = false;
};
票数 0
EN

Stack Overflow用户

发布于 2016-04-10 12:34:31

试试这样的东西。

主计长:

代码语言:javascript
复制
var truncateText = true;

$scope.renderHtml = function(content){
  // your code here
  return truncateText ? $filter('truncate')(content, 90) : content;
};

$scope.toggleTruncate = function(){
  truncateText = !truncateText;
};

查看:

代码语言:javascript
复制
<p class="forum-content" id="{{post.id}}" ng-bind-html="renderHtml(post.content)"></p>

<button class="button button-block" ng-click="toggleTruncate()">Toggle Truncate</button> 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36529582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档