Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJS: how to use a factory with two-way data binding
    primarykey
    data
    text
    <p>EDIT: Working plunker: <a href="http://plnkr.co/edit/iInh7TnbGvSHSPOWuWPk?p=preview" rel="nofollow noreferrer">http://plnkr.co/edit/iInh7TnbGvSHSPOWuWPk?p=preview</a></p> <p>====================================================================</p> <p>In the following plunker: <a href="http://plnkr.co/edit/TIfkYE?p=preview" rel="nofollow noreferrer">http://plnkr.co/edit/TIfkYE?p=preview</a></p> <p>A very simple example compares the usage of a factory and a service. Two way data-binding works using the service but not the factory. Why?</p> <p>With the service:</p> <pre><code>&lt;button btn-radio="item.nb" ng-model="fromService.current&gt; </code></pre> <p>clicking the button correctly updates the property current of the service fromService.</p> <p>However, using a factory:</p> <pre><code>&lt;button btn-radio="item.nb" ng-model="fromFactory.current&gt; </code></pre> <p>clicking the button does not update the property current of the factory fromFactory.</p> <p>My use case is the following: I want to use a service/factory to share data across controllers and other services. </p> <p>I have read a lot references, including:</p> <ul> <li><a href="http://iffycan.blogspot.be/2013/05/angular-service-or-factory.html" rel="nofollow noreferrer">http://iffycan.blogspot.be/2013/05/angular-service-or-factory.html</a></li> <li><a href="http://docs.angularjs.org/api/AUTO" rel="nofollow noreferrer">http://docs.angularjs.org/api/AUTO</a>.$provide</li> <li><a href="https://gist.github.com/Mithrandir0x/3639232" rel="nofollow noreferrer">https://gist.github.com/Mithrandir0x/3639232</a></li> <li><a href="https://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript">What is the &#39;new&#39; keyword in JavaScript?</a></li> <li><a href="http://zeekat.nl/articles/constructors-considered-mildly-confusing.html" rel="nofollow noreferrer">http://zeekat.nl/articles/constructors-considered-mildly-confusing.html</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload