如何使指令重新渲染视图角? [英] How to make directive re-render view in Angular?

查看:137
本文介绍了如何使指令重新渲染视图角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想截断动态文本,在每个数据变化和每个窗口的resize事件。

I would like to truncate text dynamically, on each data change and each window's resize event.

让我们说我有一个HTML:

Lets's say I have a HTML:

<p ng-truncate='lines: 2'> Lorem ipsum dolor...</p>

我的指令做截断,但它仍然在窗口的大小调整缺乏再截断。

My directive does the truncation, but still it lacks of re-truncation on window's resize.

angular.module('moduleName', [])
  .directive 'ngTruncate', () ->
    link: (scope, element, attributes) ->
      // Direcive code here
      $(window).on 'resize', ->
        scope.$apply()
        scope.$digest()

可惜的是, $适用,也不 $消化()不起作用。

此外,我相信我应该使用 $窗口不知何故...

Moreover, I believe I should use $window somehow...

推荐答案

也许没有在 $范围发生了变化你打电话之前 $适用 $消化上调整大小。

Probably nothing has changed in $scope before you called $apply or $digest on resize.

更好的添加一些功能,例如:

Better add some function, eg.:

function onWindowChange() {
    // do some changes to any 
    // attributes of scope
    // and than apply them
    scope.$digest()
}

// and call it on window resize
$window.resize(onWindowChange);

或者你可能有功能至极将改变
任何范围ATTR,至极调用许多
不同的事件和 $范围。$看此ATTR
和变化呼叫 $范围。$摘要

Or you may have function wich will change any scope attr, wich you call on many different events, and $scope.$watch this attr and on change call $scope.$digest.

也许会更容易回答,如果你发布指令,整个code在这里...

Maybe it would be easier to answer if you posted whole directive code here...

这篇关于如何使指令重新渲染视图角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆