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:
However, the code happily works and data is bound correctly. Can someone help untangle why this isn't throwing an error for me?
Made my original comment because I wasn't 100%, but after checking the docs, it's because...
"ngModel will try to bind to the property given by evaluating the expression on the current scope. If the property doesn't already exist on this scope, it will be created implicitly and added to the scope."
So if that property doesn't exist, it will create when then assign the value to it.
src: here