如何在angularjs的子模块中引用控制器 [英] how to reference a controller inside a sub-module in angularjs

查看:29
本文介绍了如何在angularjs的子模块中引用控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据对此 问题 我应该在主模块中引用子模块,这应该可以解决问题.>

这是我用于引导应用程序的代码:

I'm using modules /sub modules on the angular app, my controller doesn't load on a specific route but the view does, according to a comment on this question I should reference the child module inside the main module and that should do the trick.

this is my code for bootstrapping the app:

我在一个名为 /subModule1/submodule1.js 的单独文件夹中有子模块配置

I have the sub-module configuration in a separate folder named /subModule1/submodule1.js

控制器定义为:

the controller is defined as:

位于页面根部的索引是页面布局有

angular.module('subModule1').controller('DashboardCtrl', function ($scope, $interval, $resource, notification, dashboardinfo) { ... }

the index located on the root of the page which is the page layout have the 

并且控制器具有如下的 ng-controller 定义:

<html ng-app="mainApp">

and the controller have the ng-controller definiton  as follows:

一切都很好,只是控制器没有运行,视图没有执行它.

Everything is fine just the controller isn't running, it doesn't get executed by the view.

推荐答案

ui-routerng-controller="DashboardCtrl" 旨在协同工作.在 ui-router 世界中,我们直接在状态定义中将 Controllers 分配给 views.

The ui-router and ng-controller="DashboardCtrl" are intended to work together. In the ui-router world we are assigning Controllers to views directly in the state definition.

所以这个(就像你已经拥有的一样,没有变化)就足够了:

.state('submodule1.dashboard',
{
    url: '/dashboard',
    views: {
        'container@': {
            templateUrl: '/subModule1/partial/dashboard/dashboard.html',
            controller: 'DashboardCtrl',

也就是说,在根 (index.html) 上的 ui-view="container" 内部呈现的视图应该与 DashboardCtrl 一起提供.

to say, that the view rendered inside of the ui-view="container" on the root (index.html) should be provided with DashboardCtrl.

有一个示例使用上述状态定义(1:1尽可能).

There is an example using the above state definition (1:1 as possible).

这是 index.html 内容:

This is the index.html content:

<div ui-view="header"></div>
<div ui-view="navBar"></div>
<div ui-view="container"></div>
<div ui-view="sideBar"></div>
<div ui-view="footer"></div>

而这个链接会正确触发上述状态:

And this links will correctly trigger the above states:

// root
<li><a ui-sref="root.home">root.home</a></li>
<li><a ui-sref="root.login">root.login</a></li>

// dashboard
<li><a ui-sref="submodule1.dashboard">submodule1.dashboard</a></li>

所有其他详细信息请查看此处

All the other details check here

这篇关于如何在angularjs的子模块中引用控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆