Skip to content

Instantly share code, notes, and snippets.

@aeisenberg
Created August 1, 2014 20:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeisenberg/bd0b97a97a71606cfde6 to your computer and use it in GitHub Desktop.
Save aeisenberg/bd0b97a97a71606cfde6 to your computer and use it in GitHub Desktop.
Nested scopes in angular behave strangely because of prototypal inheritance.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
angular.module('watchingApp', [])
.controller('watchedController', function($scope) {
$scope.value = 9;
})
.directive('watching', function() {
return {
restrict: 'E',
scope: {
value: '='
},
link: function(scope, element, foo) {
scope.clicked = function() {
scope.value = scope.value+1;
};
},
template: '<p>My value is {{value}}<br/><button ng-click="clicked()">Click me!</button></p>'
};
});
</script>
</head>
<body id="watchingApp">
<div ng-controller="watchedController">
Value is {{value}}. This value and the next are always the same.
<watching value="value"></watching>
<div ng-if="true">
These next two values start the same, but after clicking one of their buttons,
they diverge from the outer values.
<watching value="value"></watching>
<watching value="value"></watching>
What is going on?
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment