当我检查$scope
时,我看到firstForm
但我在对象中看不到secondForm
。我不知道为什么会发生这种情况。
这里是沙箱代码:
的index.html
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<script>
var firstController = function ($scope){ };
</script>
</head>
<body>
<div ng-controller="firstController">
<form name="firstForm"></form>
<div ng-include="'second_form.html'"></div>
</div>
</body>
</html>
second_form.html
<form name="secondForm"></form>
答案 0 :(得分:1)
感谢@charlietfl提出这个建议:
可能会考虑使用ng-include切换一个简单的指令,只有templatetUrl然后才能拥有该子范围
<强> 的index.html 强>
<html ng-app="myModule">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<script>
var myModule = angular.module('myModule', []);
myModule.directive('secondForm', function () {
return{
templateUrl: 'secondform.html'
}
});
var firstController = function ($scope){
console.log($scope);
};
</script>
</head>
<body>
<div ng-controller="firstController">
<form name="firstForm">
</form>
<div second-form></div>
</div>
</body>
这有效!
答案 1 :(得分:0)
ng-include
将创建另一个$scope
。如果您希望firstController()
处理两种表单,请执行以下操作:
<div>
<form ng-controller="firstController" name="firstForm"></form>
<div ng-include="'second_form.html'"></div>
</div>
然后在second_form.html
做:
<form ng-controller="firstController" name="secondForm"></form>
答案 2 :(得分:0)
将 ng-include 指令用于其他控制器。 在secondForm.html中将控制器名称指定为
a:yournewdomainname.com
所以你在主控制器中使用ng-inculde ng-include中的子模板的所有控制器都将从主控制器继承,即它将具有父子关系..
更多信息请访问此链接
ng-include and ngRoute: how to make them work together? (i.e. route to a view wihin a ng-include)