Skip to content

Instantly share code, notes, and snippets.

@fdietz
Created February 19, 2013 19:20
Show Gist options
  • Save fdietz/4988958 to your computer and use it in GitHub Desktop.
Save fdietz/4988958 to your computer and use it in GitHub Desktop.
Recipes with Angular.js: Controllers - Sharing Models Between Nested Controllers
var app = angular.module("MyApp", []);
app.controller("MyCtrl", function($scope) {
$scope.name = "Peter";
$scope.user = {
name: "Parker"
};
});
app.controller("MyNestedCtrl", function($scope) {
});
<html>
<head>
<script src="js/angular.js"></script>
<script src="js/app.js"></script>
<link rel="stylesheet" href="css/bootstrap.css">
<style>
.nested {
border: 1px solid red;
margin-left: 2em;
padding: 1em;
}
</style>
</head>
<body ng-app="MyApp">
<div ng-controller="MyCtrl">
<label>Primitive</label>
<input type="text" ng-model="name">
<label>Object</label>
<input type="text" ng-model="user.name">
<div class="nested" ng-controller="MyNestedCtrl">
<label>Primitive</label>
<input type="text" ng-model="name">
<label>Primitive with explicit $parent reference</label>
<input type="text" ng-model="$parent.name">
<label>Object</label>
<input type="text" ng-model="user.name">
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment