Note that there are some explanatory texts on larger screens.

plurals
  1. POsharing a var between views/controllers
    primarykey
    data
    text
    <p>I know this question has been asked 100s of times but i have yet to succeed in implementing this after reading countless of threads... shame on me.</p> <p>I know this is a lot of code (sorry). But I just can't find what I'm doing wrong, So to the problem I want to add something to the list in pat1 then click over to pat2 and see the same list.</p> <p>Routing;</p> <pre><code>var routeApp = angular.module('routeApp', ['ngRoute', 'rootMod']); routeApp.config(['$routeProvider', function($routeProvider) { $routeProvider. when('/pat1', { templateUrl: 'partials/pat1.html', controller: 'pageOneCont' }). when('/pat2', { templateUrl: 'partials/pat2.html', controller: 'pageTwoCont' }). otherwise({ redirectTo: '/pat1' }); }]); </code></pre> <p>Controllers &amp; Service:</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; }; aService.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; }; aService.registerObserverCallback(updateFoo); //service now in control of updating foo }; }]); /* Service */ rootMod.factory('serv', [ function () { var observerCallbacks = []; //register an observer this.registerObserverCallback = function(callback){ observerCallbacks.push(callback); }; //call this when you know 'foo' has been changed var notifyObservers = function(){ angular.forEach(observerCallbacks, function(callback){ callback(); }); }; }]); </code></pre> <p>INDEX.html</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;/body&gt; &lt;!-- SinglePage --&gt; &lt;div ng-view&gt;&lt;/div&gt; &lt;/html&gt; </code></pre> <p>PAT1.html</p> <pre><code>&lt;!doctype html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;Angular Test&lt;/title&gt; &lt;link rel="stylesheet" href="css/app.css"&gt; &lt;link rel="stylesheet" href="css/bootstrap.css"&gt; &lt;script src="lib/angular/angular.js"&gt;&lt;/script&gt; &lt;script src="js/controllers.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;div &gt;&lt;!--ng-app="myModule" ng-controller="ContactController"&gt;--&gt; &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; &lt;/div&gt; &lt;/html&gt; </code></pre> <p>PAT2.html</p> <pre><code>&lt;!doctype html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;Angular Test&lt;/title&gt; &lt;link rel="stylesheet" href="css/app.css"&gt; &lt;link rel="stylesheet" href="css/bootstrap.css"&gt; &lt;script src="lib/angular/angular.js"&gt;&lt;/script&gt; &lt;script src="js/controllers.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;div&gt; &lt;!--ng-app="myModule" ng-controller="ContactController"&gt;--&gt; &lt;h1&gt; TWO &lt;/h1&gt; &lt;div ng-repeat="item in foo"&gt;{{ item }}&lt;/div&gt; &lt;/div&gt; &lt;/html&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.
 

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