Skip to content

Instantly share code, notes, and snippets.

@mattraykowski
Created September 15, 2016 20:21
Show Gist options
  • Save mattraykowski/0f4cc954b3d5e14cf954d3b3dc5a207a to your computer and use it in GitHub Desktop.
Save mattraykowski/0f4cc954b3d5e14cf954d3b3dc5a207a to your computer and use it in GitHub Desktop.
JavaScript references make Angular hell.
function controllerOne(varA) {
var vm = this;
vm.a = varA;
vm.printA = function() {
console.log('A is: ', vm.a)
}
vm.setA = function(a) {
vm.a.a = a;
console.log('A is: ', vm.a)
}
}
function controllerTwo(varB) {
var vm = this;
vm.b = varB;
vm.printB = function() {
console.log('B is: ', vm.b)
}
vm.setB = function(b) {
vm.b = b;
console.log('B is: ', vm.b)
}
}
// Our mock data.
var a = { a: 'a' };
// Create our two controllers so they're using our mock data.
var num1 = new controllerOne(a);
var num2 = new controllerTwo(a);
// Prove that they're using the mock data.
num1.printA();
num2.printB();
// Mutate data within the data structure.
num1.setA('foo');
// Show that it reflects in the second controller.
num2.printB();
// Now change the data structure within B.
num2.setB({a: 'b'});
// Show that it's changed in B.
num2.printB();
// And that A is now orphaned:
num1.printA();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment