简体   繁体   English

AngularJS UI Bootstrap Modal将对象传递给控制器

[英]AngularJS UI Bootstrap Modal passing object to controller

I'm trying to pass an object to the modal controller with resolve but it doesn't appear to pass correctly. 我正在尝试使用resolve将对象传递给模式控制器,但它似乎无法正确传递。 I can console.log the object fine right before I pass it, but trying to log it in the modal controller just shows it is undefined. 我可以在传递对象之前立即console.log对象,但是尝试将其记录在模式控制器中只是显示它是未定义的。 I've looked at other related questions but I can't see what I'm doing differently from the answers they've been given. 我看过其他相关的问题,但是从给出的答案中看不出我在做什么。 Here's my controllers: 这是我的控制器:

app.controller('BlogController', ['$scope', '$http', '$modal', function($scope, $http, $modal){

    $scope.blogEntry = {}; // Place holder for data (blog entry)

    $scope.editBlogEntry = function(blogEntry) {
        $scope.blogEntry = blogEntry;
        $scope.editModal = $modal.open({
           templateUrl: '/resources/partials/editBlogModal.html',
            controller: 'EditBlogController',
            size: 'lg',
            resolve: {
                blogEntry: function() {
                    console.log($scope.blogEntry);  //this shows the object
                    return $scope.blogEntry;
                }
            }
        });
    };
}]);

app.controller('EditBlogController', ['$scope', '$http', '$modalInstance', function($scope, $http, $modalInstance, blogEntry){
    $scope.blogEntry = blogEntry;
    console.log($scope.blogEntry);  //undefined
}])

Can anyone see what I'm missing? 谁能看到我所缺少的吗? Really appreciate the help! 非常感谢您的帮助!

You forgot to add blogEntry as the last string in the array passed to the modal controller. 您忘记将blogEntry添加为传递给模式控制器的数组中的最后一个字符串。

app.controller('EditBlogController', ['$scope', '$http', '$modalInstance', function($scope, $http, $modalInstance, blogEntry)
                                                                     HERE ^

Do yourself a favor, and use ng-annotate , which will remove the need for this ugly array syntax, and thus avoid those kinds of bugs. 帮自己一个忙,并使用ng-annotate ,它将消除对这种难看的数组语法的需要,从而避免了此类错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM