[英]How to access a variable defined in an included file with angularjs?
I have a form like this: 我有这样的形式:
my_file.html: my_file.html:
<form name='first_form'>
...
</form>
<div ng-include="'/includes/second_form.html'"></div>
<a ng-click="myLog(first_form);myLog(second_form);">Submit</a>
/includes/second_form.html : /includes/second_form.html :
<form name='second_form'>
...
</form>
In can pass first_form
to my controller, but not second_form
. 可以将
first_form
传递给我的控制器,但不能second_form
。 (myLog does a console.log). (myLog执行console.log)。
How can I access the form in the included file ? 如何访问包含文件中的表格? Is there a better way to have access to the forms in angular ?
有没有更好的方法可以访问angular的表单?
The principle is this: 原理是这样的:
Wrap my_file.html
in a directive and include the second form as a directive too, eg as: 将
my_file.html
包裹在指令中,并将第二种形式也包含在指令中,例如:
<div outer-directive>
...
<div inner-directive></div>
...
</div>
The outerDirective
must define a controller with a member method, eg setInnerForm(form)
, that will store its form
argument in the scope. outerDirective
必须使用成员方法(例如setInnerForm(form)
定义一个控制器,该方法会将其form
参数存储在范围中。 The inner directive will require the parent controller and call this method as: 内部指令将需要父控制器并将此方法调用为:
app.directive("innerDirective", function() {
return {
...
templateUrl: "/includes/second_form.html",
require: "^outerDirective",
link: function(scope, elem, attrs, outerController) {
outerController.setInnerForm(scope.second_form);
},
...
};
}
A working demo of the principle is here: http://jsfiddle.net/ABK5y/ 该原理的工作演示在这里: http : //jsfiddle.net/ABK5y/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.