简体   繁体   English

AngularJS多次设置同一个ngapp

[英]AngularJS setting the same ngapp more than once

The problem I am having is I need the root element as an anchor tag, then need a div under the anchor tag. 我遇到的问题是我需要根元素作为锚标记,然后在锚标记下需要div。 They will both be using the same angular controller which belongs to the same app. 他们都将使用属于同一应用的相同角度控制器。 The databind on vm.Open works find inside the anchor tag, but it is not working inside the div tag. vm.Open上的databind可以在anchor标记内找到,但在div标记内则无法工作。 How can I have the div tag also bootstrap as 'app' with the controller 'ordercontrol'? div标签如何与控制器“ ordercontrol”一起引导为“ app”? Right now I have : 现在我有:

My HTML 我的HTML

<a data-ng-app="app" data-ng-controller="ordercontrol as vm" href="#" data-ng-click="vm.Open = !vm.Open">{{vm.Open}}</a>

<div data-ng-app="app" data-ng-controller="ordercontrol as vm" id="QuickOrderDiv">
    <div class="row">
        {{vm.Open}} //showing as '{{vm.Open}}' inside page
    </div>
</div>

Per AngularJS documentation , only one application can be bootstrapped per HTML document. 根据AngularJS文档 ,每个HTML文档只能引导一个应用程序。 There is probably not a good reason for you to try and declare and use multiple apps in one document. 您可能没有充分的理由尝试在一个文档中声明和使用多个应用程序。 Depending on your intention, there are a few ways to proceed. 根据您的意图,有几种方法可以进行。

First, remember: assigning a controller to an element (using the ng-controller directive) creates a new scope that inherits from the parent. 首先,请记住:将控制器分配给元素(使用ng-controller指令)会创建一个从父级继承的新作用域 All elements, directives, and additional controllers used within that scope can use that controller to share functionality. 在该范围内使用的所有元素,指令和其他控制器都可以使用该控制器共享功能。 So in essence, a controller is used to centralize models and application logic that is specific to an application and usually to a view (think of any functional task in your application, such as an order form or log in page; those are serviced by controllers). 因此,从本质上讲,控制器用于集中特定于应用程序和视图的模型和应用程序逻辑(例如,应用程序中的任何功能任务,例如订单或登录页面;这些由控制器提供服务) )。

If you want to reuse a behavior multiple times throughout your application, and that behavior is not specific to a particular view in your application (think of a component, like a date picker, or perhaps a shopping cart status icon/link) you may wish to encapsulate your logic in a directive . 如果您想在整个应用程序中多次重复使用某个行为 ,并且该行为并非特定于您的应用程序中的特定视图(例如组件,例如日期选择器或购物车状态图标/链接),您可能希望将逻辑封装在指令中

Now, there is some overlap (for example, directives can have a scope or controller of their own), and it may be confusing when to use one or the other. 现在,存在一些重叠(例如,指令可以具有自己的作用域或控制器),并且何时使用一个或另一个可能会造成混淆。 As I mentioned above, controllers are primarily intended to contain business logic for a view in your application. 如上所述,控制器主要用于包含应用程序中视图的业务逻辑。 Directives are more orthogonal, encapsulated templates and logic that a) you can easily reuse across the views in your application, and b) extend existing HTML elements with richer mark-up and programmed behaviors. 指令是更正交的,封装的模板和逻辑,它们可以:a)您可以轻松地在应用程序中的视图之间重用,b)扩展现有HTML元素,使其具有更丰富的标记和编程行为。 You can use a service (which is a single-instance object) to coordinate data between controllers and directives. 您可以使用服务(它是单实例对象)来协调控制器和指令之间的数据。

Another common issue new developers struggle with is maintaining or inheriting different states independently, considering that you can only have one ng-view element per document. 考虑到每个文档只能有一个ng-view元素,新开发人员面临的另一个常见问题是独立维护或继承不同的状态。 In that case, consider ui-router . 在这种情况下,请考虑ui-router

My wild guess for your case, you may want some sort of QuickOrder directive that binds to a value on OrderController to determine whether or not it should display, and contains additional template mark-up for displaying the order or whatever and the logic to manage it. 我胡乱猜测你的情况,你可能需要某种QuickOrder指令其结合到一个值OrderController ,以确定它是否应该显示,并包含更多的模板加价显示的顺序或任何和管理它的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM