Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It happens because the factory should be defined by different way.</p> <p><em>(I took dummy URL only to load data by async way)</em></p> <p><strong>HTML</strong></p> <pre><code>&lt;div ng-controller="NotificationBoxController"&gt; &lt;button ng-click="showMe();"&gt;press me&lt;/button&gt; &lt;pre&gt;NotificationBoxController: {{items.getList()|json}}&lt;/pre&gt; &lt;/div&gt; &lt;div ng-controller="NotificationController"&gt; &lt;pre&gt;NotificationController: {{items.getList()|json}}&lt;/pre&gt; &lt;/div&gt; </code></pre> <p><strong>JS</strong></p> <pre><code>var pqsAppModule = angular.module('myApp', []); pqsAppModule.controller('NotificationBoxController',function($scope,items) { $scope.items = items; $scope.showMe= function(){ items.query(); } }); pqsAppModule.controller('NotificationController',function($scope,items) { $scope.items = items; }); pqsAppModule.factory('items', function ($http) { var current = {}; var address = 'Singapore, SG, Singapore, 153 Bukit Batok Street 1'; var URL = 'http://maps.googleapis.com/maps/api/geocode/json?address=' + address + '&amp;sensor=true'; var factory = { query: function () { var data = $http({method: 'GET', url:URL}).then(function (result) { current = result.data.results[0]; }, function (result) { alert("Error: No data returned"); }); }, getList: function () { return current; } } return factory; }); </code></pre> <p>Demo <kbd><strong><a href="http://jsfiddle.net/PxdSP/1012/" rel="nofollow">Fiddle</a></strong></kbd></p> <p>From this example we call <code>items.getList()</code> from HTML for both controllers. But if we want to update the model through the controller, we need a listener like <code>$watch</code></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.
 

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