如何在角度boostrap模态的控制器中注入工厂?

时间:2015-05-18 11:08:35

标签: angularjs angular-ui-bootstrap

我想在“提交”按钮上创建确认对话框。所以我使用角度ui bootstrap。

这是我的主控制器代码:

FrontEnd.controller('PostViewController',function($scope,$stateParams,PostFactory, $modal){

    $scope.openConfirm = function(size) {

            var modalInstance = $modal.open({
                animation: true,
                templateUrl: 'ConfirmDialog.html',
                controller: 'ConfirmDialogController',
                size: size, 
                resolve: {
                    form: function () {
                            return $scope.form;
                        },
                    //tried with this line as well, but could not resolve error
                    PostFactory: "PostFactory"   
                }
            });

            modalInstance.result.then(function (response) {
                $scope.isSurveySubmitted = true;
                $scope.message = response.message;

                //$scope.selected = selectedItem;
              }, function () {
                console.log('Modal dismissed at: ');
            });
    };
});

这是我的ConfirmDialogController:

FrontEnd.controller('ConfirmDialogController', function($scope, $http, $modalInstance, form, PostFactory){

    $scope.ok = function () {

        PostFactory.SubmitSurvey(form).success(function(response){
            $modalInstance.close(response);
        }).success(function(response){
            $modalInstance.close(response);
        });
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});

模态正确打开,没有错误。当我点击取消按钮时,它关闭模式正如我所料。但是当我点击确定时,它会调用 $ scope.ok(),但会在控制台中抛出错误消息,PostFactory未定义。

那该怎么办?谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

我认为您可以使用scope

中的$modalInstance.open()字段

将工厂设置为新范围的属性

var scope = $scope.$new();
scope.factory = PostFactory;
$modalInstance.open({
    // other stuff
    scope: scope
});

然后在yoou modal的控制器中引用$ scope.factory。

不确定是否会弄乱其他任何东西,值得一试。

相关问题