简体   繁体   English

Angular UI路由器控制器范围

[英]Angular ui-router controller scope

When I use a regular Angular controller, I place the controller attribute inside a DOM element, and the controller controls all DOM tree inside this element, eg 当我使用常规的Angular控制器时,我将controller属性放置在DOM元素内,并且控制器控制该元素内的所有DOM树,例如

<div ng-controller="myController">

When I attach a controller via ui-router I do it in my app.js file, via the states config, like this: 当我通过ui-router附加控制器时,我会通过状态配置在app.js文件中执行此操作,如下所示:

.state('report', {
    url: '/site/:site/report',
    templateUrl: 'minderbinder/report/view/report.html',
    controller: 'ReportController'
})

What is the scope of this controller? 该控制器的范围是什么?

You shouldn't declare your controller manually in your div , unless your intent is to make a child (controller) of the ReportController to control some nested and specific part of your DOM. 您不应该在div手动声明控制器,除非您的意图是使ReportController的子代(控制器)控制DOM的某些嵌套和特定部分。

As a child, then myController would inherit from ReportController 's scope, this latter being the upmost controller of your template. 小时候, myController将从ReportController的范围继承,后者是模板的最高控制器。

The benefit of avoiding to declare controller in your HTML is to promote reusability of your HTML template across potential various controllers, using the exact same HTML. 避免在HTML中声明控制器的好处是,可以使用完全相同的HTML在潜在的各种控制器中提高HTML模板的可重用性。

A typical example would be an edit form and an adding form (CRUD) mapped to their own distinct controllers but the same HTML template. 一个典型的示例是将编辑表单和添加表单(CRUD)映射到它们自己的不同控制器,但使用相同的HTML模板。

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

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