我有一个简单的ASP.NET MVC应用程序,它包含一个ng控制器。 使用局部视图我在此控制器内部注入了另一个仅在需要时才使用的ng控制器。我怎样才能使它工作,因为我无法正确地进行绑定。这是一个plunker与我需要的简化版本。动态插入嵌套控制器
<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>
和JavaScript部分:
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"
};
}