Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>JS</h2> <ol> <li><p>Your service is injected as <code>serv</code> in your controllers but you call <code>aService</code>. Use <code>serv</code> instead. </p></li> <li><p>You should use <code>rootMod.service()</code> instead of <code>rootMod.factory()</code> since you are using <code>this</code> and not returning anything in your service function (see <a href="https://stackoverflow.com/a/15914263/1796523">this</a> for the difference between factories and services).</p></li> <li><p>There is no <code>serv.foo</code> property. You have to add <code>this.foo = /** something **/;</code> to your service function.</p></li> </ol> <p>So this should work:</p> <pre><code>var rootMod = angular.module('rootMod', []); rootMod.controller('pageOneCont', [ 'serv', '$scope', function (serv, $scope) { 'use strict'; // Your controller implementation goes here ... $scope.handleClick = function ($scope, serv){ var updateFoo = function(){ $scope.foo = serv.foo; }; serv.registerObserverCallback(updateFoo); // service now in control of updating foo }; }]); rootMod.controller('pageTwoCont', [ 'serv', '$scope', function (serv, $scope) { 'use strict'; // Your controller implementation goes here ... $scope.handleClick = function ($scope, serv){ var updateFoo = function(){ $scope.foo = serv.foo; }; serv.registerObserverCallback(updateFoo); // service now in control of updating foo }; }]); /* Service */ rootMod.service('serv', [ function () { var observerCallbacks = []; // call this when you know 'foo' has been changed var notifyObservers = function(){ angular.forEach(observerCallbacks, function(callback){ callback(); }); }; // initialize foo here this.foo = ''; // register an observer this.registerObserverCallback = function(callback){ observerCallbacks.push(callback); }; }]); </code></pre> <h2>HTML</h2> <p>You put the <code>ng-view</code> container outside of the <code>body</code>. It has to be inside. Also the path to a view starts with <code>#/</code> so you have to change your links.</p> <pre><code>&lt;!doctype html&gt; &lt;html lang="en" ng-app="routeApp"&gt; &lt;head&gt; &lt;script src="lib/angular/angular.js"&gt;&lt;/script&gt; &lt;script src="lib/angular/angular-route.js"&gt;&lt;/script&gt; &lt;script src="js/app.js"&gt;&lt;/script&gt; &lt;script src="js/controllers.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;li&gt;&lt;a href="#/pat1"&gt; ONE &lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#/pat2"&gt; TWO &lt;/a&gt;&lt;/li&gt; &lt;!-- SinglePage --&gt; &lt;div ng-view&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Each view only needs the content that should be placed inside the <code>ng-view</code> container. So you mustn't make a complete HTML document.</p> <p><strong>pat1.html</strong></p> <pre><code>&lt;h1&gt; ONE &lt;/h1&gt; &lt;button ng-click="handleClick"&gt; BROADCAST &lt;/button&gt; &lt;div ng-repeat="item in foo"&gt;{{ item }}&lt;/div&gt; </code></pre> <p><strong>pat2.html</strong></p> <pre><code>&lt;h1&gt; TWO &lt;/h1&gt; &lt;div ng-repeat="item in foo"&gt;{{ item }}&lt;/div&gt; </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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