[英]Can I have multiple controller inside1 Div in Angular JS?
In Angular JS, I have a section of form where I want to use 2 different controllers. 在Angular JS中,我有一部分要使用2个不同的控制器的形式。 Can I have multiple controllers inside 1 directive like this?
我可以在1个指令中包含多个控制器吗?
<form ng-controller="DatePickerController FileUploadController">
I tried the above and its not working. 我尝试了以上,但不起作用。 Is this not allowed?
这是不允许的吗?
The ng-controller directive creates a $scope for the element it's on. ng-controller指令为其所在的元素创建一个$ scope。 The text inside ng-controller references the specific namespace you bound your controller code to.
ng-controller中的文本引用绑定了控制器代码的特定名称空间。 So this would be the equivalent of trying to find your
myApp["DatePickerController FileUploadController"]
controller. 因此,这等效于尝试找到
myApp["DatePickerController FileUploadController"]
控制器。 Not going to work . 不上班 。
There's also a second thing I've noticed: you're referencing datepicker and fileupload controllers. 我还注意到了第二件事:您引用的是datepicker和fileupload控制器。 To me, both of those sound like directives.
对我来说,这两个听起来像是指令。 Controllers are for views - aka they control the layout of a particular page.
控制器用于视图-也就是它们控制特定页面的布局。 Directives are for adding functionality to your HTML, like date picking and file uploading.
指令用于向HTML添加功能,例如日期选择和文件上传。
I would take a look at http://ngmodules.org/ , they should have the functionality you need in the correct format. 我将看一下http://ngmodules.org/ ,它们应该具有正确格式的所需功能。
No as it's written this can't be done. 不,因为它是写的,所以无法完成。 See the snippet below from the documents.
请参阅下面的文档片段。 You could write a constructor function that creates each of those controllers and abstracts the scope function calls but as is there is no way to do this (I think this may be a bit of a mess since you have to determine where to resolve a particular method if both controllers contain it and maybe other problems)
您可以编写一个构造函数来创建每个控制器,并抽象作用域函数调用,但是没有办法做到这一点(我认为这可能有些混乱,因为您必须确定在哪里解析特定方法如果两个控制器都包含它,可能还有其他问题)
ngController - expression - Name of a globally accessible constructor function or an expression that on the current scope evaluates to a constructor function.
ngController-expression-全局可访问的构造函数的名称,或者在当前作用域上计算为构造函数的表达式。 The controller instance can be published into a scope property by specifying as propertyName.
通过指定为propertyName可以将控制器实例发布到范围属性中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.