是否有不同的方法来声明与 Angular UI 路由器状态关联的控制器 [英] Are there different ways of declaring the controller associated with an Angular UI Router state

查看:14
本文介绍了是否有不同的方法来声明与 Angular UI 路由器状态关联的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 Angular UI 路由器的询问.

I have an interrogation about the Angular UI router.

顶层视图层声明与状态关联的控制器有什么不同吗?

Is it any different to declare the controller associated with the state at the top level or at the view level?

见下面的片段:

.state('signup', {
    controller: 'SignupCtrl',//HERE
    views: {
        '@': {
            controller: 'SignupCtrl',//OR THERE
            templateUrl: 'signup/views/signup.html'
        }
    }
})

声明控制器//HERE//OR THERE有什么不同吗?

Is it any different to declare the controller //HERE //OR THERE?

如果有,有什么区别?

推荐答案

有一个很大的不同 - 因为只使用了一个或另一个.事实上,最重要的信息是:

There is a big difference - because only one or the other is used. In fact, the most important message is:

控制器属于状态.它属于view

如果有 views : {} 对象,则每个 view 都可以定义自己的 controller.

In case if there is a views : {} object, each view can have its own controller defined.

.state('myState', {
    views: {
        '@': {
            controller: 'SignupCtrl',
            templateUrl: 'signup/views/signup.html'
        },
        'hint@': {
            controller: 'HintCtrl',
            templateUrl: 'signup/views/signup.html'
        }
    }
})

有一个例外视图定义.如果我们以状态父级中的 ui-view="" 为目标,就会发生这种情况.在这种情况下,我们仍然可以这样写:

There is one exceptional views definition. It happens, if we target the ui-view="" in the state parent. In that case, we can still writ it like this:

.state('myState', {
    views: {
        '': {
            controller: 'SignupCtrl',
            templateUrl: 'signup/views/signup.html'
        },
    }
})

但是我们可以使用简化版,等于这个定义:

But we can use simplified version, equal to this definition:

.state('myState', {
    controller: 'SignupCtrl',
    templateUrl: 'signup/views/signup.html'
})

所以最后两个在最后是相等的.但同样,在上一个示例中,我们只使用了简化版本.控制器总是属于视图(模板)而不是状态...

So last two are equal at the end. But again, in the last example we just used simplifed version. Controller always belong to view (template) not to state...

这篇关于是否有不同的方法来声明与 Angular UI 路由器状态关联的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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