Dynamic one-time snapping or force to calculate one-time snapping again

There is one time binding in angular 1.3 ::

To reduce observers, I want to use it in my directive.

The problem is that in some cases I need a normal binding (without ::

).

Should I use two different directives for this or is there a solution to use the 'dynamic' attribute ::

? Or maybe there is a way to force angular to calculate the ontime bindings (in this directive) again?

My custom directive looks something like this:

"<span ng-switch=\"::kolumna.typ_pokaz\">" +
            "<span ng-switch-when=\"cena_waluta\" ng-bind=\"model.ngModel|waluta:ngModel.waluta\"></span>" +
            "<span ng-switch-when=\"cena\" waluta-domyslna=\"model.ngModel\"></span>" +
            "<span ng-switch-default ng-bind-html=\"model.ngModel\"></span>" +
"</span>"

      

In most cases, it model.ngModel

may not always be ::model.ngModel

, but not always.

+3


source to share


2 answers


You can have multiple templates - one with binding once and one without - and then send them to the directive as an attribute.

app.directive('myDirective', function() {
    return {
      restrict: 'E',
      templateUrl: function($element, $attrs){
        return $attrs.tmpl;
      }
    };
});

      

And the markup will look like this:



<my-directive tmpl="/path/to/only-once-template.html></my-directive>

      

and

<my-directive tmpl="/path/to/binding-template.html></my-directive>

      

+1


source


You can use "optional" or "dynamic" one-time binding. It's a little messy, but entirely possible. Here's an example:

Inside directive / component:

<div ng-init="oneTimeBindingPrefix = oneTimeBinding ? '::' : ''">
    <div ng-bind="{{::oneTimeBindingPrefix}}{{theValue}}"></div>
</div>

      



Using directive / component:

<wt-wish-added-promised-info one-time-binding="false" ...></wt-wish-added-promised-info>

      

Source

+1


source







All Articles