Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>See UPDATE below.</strong></p> <p>The $scope.$apply() function is used to put code back into the "AngularJS world".</p> <p>When using third party libraries like you are doing, that is exactly what you need to use to execute the code inside AngularJS digest cycle. When you use AngularJS services that have a callback, they already call $apply() in the background and put the code back into AngularJS digest cycle.</p> <p>In your case, you created your own service and are passing a callback to it in your controller. When that callback is called, you are already outside of an AngularJS digest cycle and therefore AngularJS will not notice that the scope changed. Calling the $apply function sets the code back to AngularJS digest cycle and let's AngularJS see that the scope changed.</p> <p>A more detailed guide to $scope.$apply can be found at <a href="http://jimhoskins.com/2012/12/17/angularjs-and-apply.html" rel="nofollow">http://jimhoskins.com/2012/12/17/angularjs-and-apply.html</a></p> <p>So, you can change your factory code like this to include the $apply call:</p> <pre><code>sample.factory('datacontext', ['breeze', 'Q', '$timeout', '$q', '$rootScope', function (breeze, q, timeout, $q, $scope) { configureBreeze(); var manager = new breeze.EntityManager("/breeze/ProductsBreeze"); manager.enableSaveQueuing(true); var datacontext = { metadataStore: manager.metadataStore, getTodoLists: getTodoLists }; function getTodoLists(onsuccess) { var query = breeze.EntityQuery.from("GetAllProducts"); manager.executeQuery(query).then($rootScope.$apply(onsuccess)); }; return datacontext; </code></pre> <p>That way you don't need to call it on every controller that uses your service.</p> <p><strong>UPDATE:</strong></p> <p>I was misguided by your sentence regarding the $scope.$apply(). That is why it is better to provide a working example, so we can play around with it.</p> <p>I think the problem is that you are changing the reference that the $scope.Products variable is set. When you initialize it with</p> <pre><code>$scope.Products = [] </code></pre> <p>you create an array A and make Products refer to that array. Then you change the array that Products is referencing when you make in your callback</p> <pre><code>$scope.Products = data.results; </code></pre> <p>Try to go through the data.results array and push it to the Products array and see if that helps.</p>
    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.
    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