I have a simple ASP.NET MVC application which contains a ng-controller. Using a partial view I inject inside this controller another ng-controller used only when needed. How can I make it work because I could not make the binding properly. Here's a plunker with a simplified version of what I need.
<body ng-app="MyApp">
<div id='parent' ng-controller="MyCtrl">
<label>Primitive</label>
<input type="text" ng-model="name">
<label>Object</label>
<input type="text" ng-model="user.name">
<button onclick="addNested();">Add Nested Controller</button>
</div>
</body>
And the javascript part:
var app = angular.module("MyApp", []);
app.controller("MyCtrl", function($scope) {
$scope.name = "ParentName";
$scope.user = {
name: "Peter"
};
});
function addNested() {
$('#parent').append(
'<div class="nested" ng-controller="MyNestedCtrl">'+
'<label>Primitive</label>' +
'<input type="text" ng-model="name"><br />' +
'<label>Primitive with explicit $parent reference</label> <br />' +
'<input type="text" ng-model="$parent.name">' +
'<label>Object</label>' +
'<input type="text" ng-model="user.name">' +
'</div>' +
'<script type="text/javascript">' +
'var a = angular.module("MyApp");' +
'a.controller("MyNestedCtrl", ["$scope", MyNestedCtrl] );' +
'</script>');
}
function MyNestedCtrl($scope)
{
$scope.name = "ChildName";
$scope.user = {
name: "Paul"
};
}