【问题标题】:Pass data to angular's nested controller将数据传递给 Angular 的嵌套控制器
【发布时间】:2013-06-28 16:38:07
【问题描述】:

问题

我在我的应用程序中使用 UI Bootstrap 的 dialog 服务,该服务创建模态对话框,我在外部 $scope 上使用以下方法:

$scope.showRouteEditDialog = function (template, route) {
    // argument `route` is not currently used

    var dialog = $dialog.dialog({
        backdrop: true,
        keyboard: false,
        backdropClick: true,
        templateUrl: template,
        controller: 'RouteController'
    });


    dialog.open().then(function(result) {
        alert(result); // This line is called when dialog is closed
    });
}

此方法稍后会使用以下标记从局部视图中调用

<a href="#" ng-click="showRouteEditDialog('Templates/Content/Timeline/Item/Editor.html', route)"><i class="halflings-icon edit"></i></a>

我的对话框处理 route 的编辑(route 是主模型中的子模型),因此我想将该路径传递给对话框,以便它像对待自己的模型一样对待它而不知道任何关于外部模型。

我对这个问题的最初猜测是将路由参数分配给dialog 变量,类似于dialog.route = route,然后在控制器中使用以下代码:

Application.controller('RouteController', ['$scope', 'dialog', function ($scope, dialog) {
    // `dialog` is our dialog and it is injected with injector
    doSomethingWith(dialog.route)
}]);

虽然这种方法会产生依赖并且看起来不像 Angular 的做事方式

我还发现 this post 说我应该为此目的使用服务,但对于这样的小问题,这种解决方案似乎有点过头了。

问题

在上述情况下,将值从外部控制器传递到内部控制器的角度方式是什么。

谢谢

【问题讨论】:

    标签: angularjs angular-ui-bootstrap


    【解决方案1】:

    你可以使用“解决” -

    var dialog = $dialog.dialog({
      resolve: {route: function(){return "route"}}
    });
    

    稍后您可以在控制器中注入“路由”值

    .controller("SomeCtrl",function(route){
    
    })
    

    【讨论】:

    • 谢谢,它成功了,但它是如何工作的?我以前没有使用“解决”。这是什么?
    • "将被解析并作为本地人传递给控制器​​的成员" - 来自:github.com/angular-ui/bootstrap/blob/master/src/dialog/… 您也可以在 ui-router 或 $route 服务中使用它作为传递特定用例的一种方式参数到通用控制器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-10
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 2016-01-30
    相关资源
    最近更新 更多