AngularJS : Why ng-bind is better than {{}} in angular?

前端 未结 12 699
灰色年华
灰色年华 2020-11-22 07:19

I was in one of the angular presentation and one of the person in the meeting mentioned ng-bind is better than {{}} binding.

One of the re

相关标签:
12条回答
  • 2020-11-22 07:19

    The reason why Ng-Bind is better because,

    When Your page is not Loaded or when your internet is slow or when your website loaded half, then you can see these type of issues (Check the Screen Shot with Read mark) will be triggered on Screen which is Completly weird. To avoid such we should use Ng-bind

    0 讨论(0)
  • 2020-11-22 07:19

    You can refer to this site it will give you a explanation which one is better as i know {{}} this is slower than ng-bind.

    http://corpus.hubwiz.com/2/angularjs/16125872.html refer this site.

    0 讨论(0)
  • 2020-11-22 07:24

    Visibility:

    While your angularjs is bootstrapping, the user might see your placed brackets in the html. This can be handled with ng-cloak. But for me this is a workaround, that I don't need to use, if I use ng-bind.


    Performance:

    The {{}} is much slower.

    This ng-bind is a directive and will place a watcher on the passed variable. So the ng-bind will only apply, when the passed value does actually change.

    The brackets on the other hand will be dirty checked and refreshed in every $digest, even if it's not necessary.


    I am currently building a big single page app (~500 bindings per view). Changing from {{}} to strict ng-bind did save us about 20% in every scope.$digest.


    Suggestion:

    If you use a translation module such as angular-translate, always prefer directives before brackets annotation.

    {{'WELCOME'|translate}} => <span ng-translate="WELCOME"></span>

    If you need a filter function, better go for a directive, that actually just uses your custom filter. Documentation for $filter service


    UPDATE 28.11.2014 (but maybe off the topic):

    In Angular 1.3x the bindonce functionality was introduced. Therefore you can bind the value of an expression/attribute once (will be bound when != 'undefined').

    This is useful when you don't expect your binding to change.

    Usage: Place :: before your binding:

    <ul>  
      <li ng-repeat="item in ::items">{{item}}</li>
    </ul>  
    <a-directive name="::item">
    <span data-ng-bind="::value"></span>
    

    Example:

    ng-repeat to output some data in the table, with multiple bindings per row. Translation-bindings, filter outputs, which get executed in every scope digest.

    0 讨论(0)
  • 2020-11-22 07:24

    {{...}} is meant two-way data binding. But, ng-bind is actually meant for one-way data binding.

    Using ng-bind will reduce the number of watchers in your page. Hence ng-bind will be faster than {{...}}. So, if you only want to display a value and its updates, and do not want to reflect its change from UI back to the controller, then go for ng-bind. This will increase the page performance and reduce the page load time.

    <div>
      Hello, <span ng-bind="variable"></span>
    </div>
    
    0 讨论(0)
  • 2020-11-22 07:24

    There is some flickering problem in {{ }} like when you refresh the page then for a short spam of time expression is seen.So we should use ng-bind instead of expression for data depiction.

    0 讨论(0)
  • 2020-11-22 07:32

    ng-bind is also safer because it represents html as a string.

    So for example, '<script on*=maliciousCode()></script>' will be displayed as a string and not be executed.

    0 讨论(0)
提交回复
热议问题