I have read several SO responses regarding Angular $scope and how it's a plain old JavaScript object, which means that it'll follow JS's prototypal inheritance (What are the nuances of scope prototypal / prototypical inheritance in AngularJS?)
Since this is the case, I'm curious why my following example DOESN'T throw an error:
<!doctype html>
<html ng-app='MyApp'>
<head>
<meta charset='utf-8'>
<title>Egghead</title>
<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js'></script>
<script>
var app = angular.module('MyApp', []);
app.controller('MainCtrl', ['$scope', function ($scope) {
}])
</script>
</head>
<body>
<div ng-controller='MainCtrl'>
<input type='text' ng-model='data.message'>
<p>{{ data.message }}</p>
</div>
</body>
</html>
Based on prototypal inheritance, here's what I would expect to happen:
- When MainCtrl is invoked, it will create a $scope object for the MainCtrl and its corresponding view.
- Whenever you type into the input box, it will bind some string to $scope.data.message.
- However, in order to do this, the first step in JavaScript will be to try to resolve $scope.data.
- Since the MainCtrl function doesn't have a data property, it will try to look for the data property in the rootScope.
- Since the rootScope doesn't have a data property, $scope.data should resolve to 'undefined'.
- Finally, if you try to assign to undefined.message, this should throw an error.
However, the code happily works and data is bound correctly. Can someone help untangle why this isn't throwing an error for me?