在那里宣布与角UI路由器状态相关联的控制器的不同方式 [英] Are there different ways of declaring the controller associated with an Angular UI Router state

查看:95
本文介绍了在那里宣布与角UI路由器状态相关联的控制器的不同方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对角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?

请参阅下面的代码片段:

See snippet below:

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

这是任何不同的申报控制器 // //此处还是存在

如果差别那么是什么呢?

If so what is the difference?

推荐答案

有一个很大的区别 - 因为只有一个或其他使用。其实,最重要的信息是:

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

控制器执行的不可以属于状态。它属于视图

controller does not belong to state. It belongs to view!

在的情况下,如果有一个的意见:{} 对象,每个视图的可以有自己的控制器定义。

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'
        }
    }
})

有一个独特景致定义。碰巧的是,如果我们的目标用户界面视图=在该州的父母。在这种情况下,我们仍然可以写它是这样的:

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'
})

所以,最后两个是在最后相等。但同样,在最后一个例子,我们只是用simplifed版本。控制器永远属于查看(模板)不声明...

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...

这篇关于在那里宣布与角UI路由器状态相关联的控制器的不同方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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