Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One time binding not working with function

I have an Angular function where i log a value

$scope.getFormattedDate = function(date){
    console.log(date)
}

and here in the html code

span {{::getFormattedDate('hello')}}

According to this the value should get rendered once and never again. But when i scroll, the value gets printed in the console continuously.

Where am i going wrong?

like image 613
Anubhav Avatar asked Feb 12 '16 17:02

Anubhav


People also ask

What is the syntax for a one-time binding?

One-time binding is used for values that won't change after the page is stable. "Stable" generally means that the expression has been assigned a value. Once the value is set, changes to the value in the controller won't change the displayed value until the page is reloaded. The syntax is {{::expression}}.

How to use 2 way data binding in Angular?

For two-way data binding, declare a private property and access its value using get and set methods in the component class. Then, assign that property to [(ngModel)] . For example, declare a private property with a get and set method, as shown below. Now, assign userName to [(ngModel)] in the template.

What is 2 way data binding?

In a two-way binding, the data flow is bi-directional. This means that the flow of code is from ts file to Html file as well as from Html file to ts file. In order to achieve a two-way binding, we will use ngModel or banana in a box syntax.

What is one-way and two-way binding?

In one-way data binding information flows in only one direction, and is when the information is displayed, but not updated. In two-way data binding information flows in both directions, and is used in situations where the information needs to be updated. They each have their uses, but most applications use both.


1 Answers

According to Angular documentation,

One-time binding expressions will retain the value of the expression at the end of the digest cycle as long as that value is not undefined

Your function is not returning anything, so the value is undefined. getFormattedDate needs to return something for Angular recognize the one-time binding.

like image 94
Chara Avatar answered Oct 01 '22 04:10

Chara