简体   繁体   English

我可以在Angular JS的1 Div中拥有多个控制器吗?

[英]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.

相关问题 我可以将角度控制器和指令放在单独的 JS 文件中吗? - Can I have the angular controller and the directive in separate JS files? 如何从promise方法中访问角度js控制器的“this”? - How can I access the “this” of an angular js controller, from inside a promise method? 如何拥有多个控制器文件? - How can I have multiple controller files? 无法访问angular.js控制器中的变量 - Can't access variable inside angular.js controller 如何在angular js的作用域函数内使用变量更改div的innerHTML? - How can I change the innerHTML of div with a variable, inside the scope function in angular js? 如何通过多次调用在Angular JS服务或控制器中建立模型数据? - how can I build up model data in Angular JS service or controller with multiple calls? 如何获取多个Angular JS路由来共享一个控制器实例? - How can I get multiple Angular JS routes to share a single controller instance? 我可以对一个问题在 js 中有多个答案吗? - Can I have multiple answers for a question in js? 如何在Angular控制器中有两个$ scoped变量? - How can I have two $scoped variables in an Angular controller? 如何为一个模板提供一个控制器的多个实例 - How can I have multiple instances of one controller for multiple templates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM