angular 手动编译,Angular:如何强制重新编译指令?

博客探讨了在HTML页面中遇到的一个问题:当代表当前页面的变量`arr`改变时,状态存储在`status-stored-in`的对象没有随之更新。目前的解决方案是在自定义指令中使用`ng-if`来强制重新编译,导致闪烁效果。作者提供了自定义指令的部分代码,并寻求更优雅的解决方式。解决方案建议在指令的链接函数中监听`status-stored-in`的变化并手动重新编译元素。
摘要由CSDN通过智能技术生成

HTML:

Problem:

I have page-turn built in for the large amount of objs. Which means that the value of arr, representing the current page of objs, will change. However, the obj in the status-stored-in="obj" part is not refreshed with the change.

Right now my solution is to add a ng-if in customDirective, flickering its value back and forth to have it force recompiled. Is there any other equivalent, neater way to deal with this?

Edit:

The start of the custom directive:

module.directive 'checkbox', (checkboxHooks) ->

restrict: 'E'

scope:

hook: '='

hookedTo: '='

statusStoredIn: '='

templateUrl: 'templates/checkbox.html'

link: (scope, element, attr) ->

The gist is that it needs to get hold of an object, for storing the checked status. The whole of it can be found here: [coffee/js].

解决方案

Inside your directives link function you need to watch status-stored-in for changes and then recompile it e.g.:

link: function(scope, element) {

scope.$watch('statusStoredIn', function() {

element.html(statusStoredIn);

$compile(element.contents())(scope);

});

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值