本文介绍了渲染值,而数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AngularJS,我怎么能不使2路数据绑定的值?有人可能会想为性能方面的原因,甚至在给定的时间点呈现一个值做到这一点。

In AngularJS, how can I render a value without 2-way data binding? One may want to do this for performance reasons, or even rendering a value at a given point in time.

下面的例子都绑定使用的数据:

< D​​IV> {{}值}< / DIV>

< D​​IV数据-NG-绑定=值>< / DIV>

我如何渲染 无任何数据绑定?

How do I render value without any data binding?

推荐答案

角1.3 +

在1.3,角已使用以下语法支持这一。

In 1.3, Angular has supported this using the following syntax.

<div>{{::message}}</div>

如这个答案。

角1.2及以下

这是简单,不需要插件。检查了这一点。

This is simple and doesn't need a plugin. Check this out.

这小指令将轻松完成你想达到什么

This small directive will easily accomplish what you are trying to achieve

app.directive('bindOnce', function() {
    return {
        scope: true,
        link: function( $scope ) {
            setTimeout(function() {
                $scope.$destroy();
            }, 0);
        }
    }
});

您可以绑定曾这样

<div bind-once>I bind once - {{message}}</div>

您可以绑定像正常

<div ng-bind="message" bind-once></div>

演示:

Demo: http://jsfiddle.net/fffnb/

你们当中有些人可能使用的角度batarang,如果你使用此指令元素仍显示为绑定时,它并不像在评论中提到的,我是pretty肯定,这事做的类,附加到元素,这样试试这个,它应该工作的(未测试)的。让我在评论中知道,如果它为你工作。

Some of you may be using angular batarang, and as mentioned in the comments if you use this directive the element still shows as binding when it is not, I am pretty sure this has something to do with the classes that are attached to the element so try this, it should work (not tested). Let me know in the comments if it worked for you.

app.directive('bindOnce', function() {
    return {
        scope: true,
        link: function( $scope, $element ) {
            setTimeout(function() {
                $scope.$destroy();
                $element.removeClass('ng-binding ng-scope');
            }, 0);
        }
    }
});

<一个href=\"http://stackoverflow.com/questions/18790333/angular-js-render-value-without-data-binding/18791503?noredirect=1#comment35565641_18791503\">@x0b:如果你有强迫症,你想删除空属性做到这一点。

@x0b: If you have OCD and you want to remove the empty class attribute do this

!$element.attr('class') && $element.removeAttr('class')

这篇关于渲染值,而数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 19:29