重用`ui-route`控制器,它需要一个`resolve`d参数 [英] Reuse `ui-route` controller that expects a `resolve`d parameter

查看:189
本文介绍了重用`ui-route`控制器,它需要一个`resolve`d参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要重用我的 ui-router 有线控制器。他们当前从 ui-router 解析接收参数以呈现其模板。我可以重复使用这些控制器没有 ui-router



例如,我用 ui-router

  .controller('DetailController',function($ scope,detailData) {
$ scope.controllerDetail = detailData +is awesome!;
})
.config(function($ stateprovider){
$ stateprovider.state('detailState' {
resolve:{
detailData:function(){returnJohn Doe;}
},
template:'< p> {{controllerDetail}}< / p>',
controller:'DetailController'
}
}

现在,我想使用相同的控制器在其他地方渲染固定的子面板。例如:

 < master ng-init ='childData =Jane Smith'> 
< detail ng-controller ='DetailController'ng-controller-params ={detailData:childData>
< p& {{controllerDetail}}< / p>
< / detail>
< / master>

当然,实际上,实际上有模板文件,复制。此外,第一个示例中的解析数据和第二个示例中的 init 数据都通过线路到达,第二个例子,它作为更大请求的子对象而不是单个导航的项目到达。

解决方案

在我的case,这是一个类似但有限的子case,我有多个 ui-router 路由与 resolve()定义, 只有一个地方 ,通过 ng-controller 在模板中实例化控制器,在这种情况下传递给控制器​​的值是一个已知的常量值。 / p>

在这种情况下,您只需要执行

  .value detailData','这里是默认值/后备值')

如果您有多个用法 ng-controller 应该有不同的 detailData ,你需要找到一个更好的解决方案。


I want to be able to reuse my ui-router-wired controllers. They currently receive parameters from ui-router resolve to render their templates. Can I reuse those controllers without ui-router?

For example, I do this with ui-router:

.controller('DetailController', function ($scope, detailData) {
    $scope.controllerDetail = detailData + "is awesome!";
})
.config(function ($stateprovider) {
    $stateprovider.state('detailState', {
        resolve: {
            detailData: function () { return "John Doe"; }
        },
        template: '<p>{{ controllerDetail }}</p>',
        controller: 'DetailController'
    }
}

Now, I want to use the same controller to render a fixed sub-panel elsewhere. For example:

<master ng-init='childData="Jane Smith"'>
    <detail ng-controller='DetailController' ng-controller-params="{ detailData : childData">
        <p>{{ controllerDetail }}</p>
    </detail>
</master>

Of course, in practice, there are actually template files, and some functionality in the controller worth de-duplicating. Also, the resolve data in the first example and init data in the second example both arrive over the wire, but in the second example it arrives as a child object of a larger request rather than an individually-navigated item. Also, I assign my controllers in directives rather via HTML attributes.

解决方案

In my case, which is a similar though limited subcase, I had multiple ui-router routes with resolve() defined, and only one place with the controller instantiated in a template via ng-controller, and the value passed to the controller in such case was a known, constant value.

In such case, you can simply do

.value('detailData', 'here goes the default/fallback value')

If you have multiple usages of ng-controller which should have different detailData, you need to find a better solution obviously.

这篇关于重用`ui-route`控制器,它需要一个`resolve`d参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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