Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Databinding a function in Angular.js

In angular I found out you can bind a template to a function which returns an array, like this:

<div class="cal_row" id ="id_{{task.id}}" ng-repeat="task in calendar.filtered()">
  <div class="id">{{task.id}}</div>
      <div class="task">{{task.task}}</div>
  <div class="start">{{task.start}}</div>
  <div class="finish">{{task.finish}}</div>
</div>

It's pretty cool, because that way I can, for example, avoid having to keep a variable around just to maintain the filtered version of the data.

However, I also loose the binding with the original data: when the underlying data changes, I can't seem to get angular.js to spot the change, and update the view.

Is there a way to do that? I tried to find anything in the docs, but couldn't

Thanks a lot

like image 727
simone Avatar asked Jul 17 '12 11:07

simone


People also ask

How do I bind a model in AngularJS?

If the model is changed, the view reflects the change and vice versa. In the above example, the {{ firstName }} expression is an AngularJS data binding expression. Data binding in AngularJS binds AngularJS expressions with AngularJS data. {{ firstName }} is bound with ng-model="firstName".

Does AngularJS support two way binding?

AngularJS creates a two way data-binding between the select element and the $ctrl.

What are the different ways of binding data in AngularJS?

As mentioned earlier, one-way data binding in Angular can be of three types i.e Interpolation, Property binding, and Event binding.

What type of binding will support by AngularJS?

Data Binding is a way to synchronize the data between the model and view components automatically. AngularJS implements data-binding that treats the model as the single-source-of-truth in your application & for all the time, the view is a projection of the model. Unlike React, angular supports two-way binding.


1 Answers

If you make a change to some data from outside angular, you have to use $myScope.$apply so angular knows something has changed. http://docs.angularjs.org/api/ng.$rootScope.Scope#$apply

like image 106
Andrew Joslin Avatar answered Oct 06 '22 17:10

Andrew Joslin