简体   繁体   English

传递的Angular UI Modal数据为null

[英]Angular UI Modal data passed is null

I'm trying to get an Angular Bootstrap UI Modal (0.14) working. 我正在尝试使Angular Bootstrap UI Modal(0.14)工作。 I can get the modal to pop up (great) but the data object I'm passing is null (it isn't null when i set it). 我可以弹出模态(很好),但是我传递的数据对象为null(设置时不为null)。 I've looked at all sorts of plukners, which I see how they work, mine just doesn;t seem to work. 我看过各种各样的plukner,我看它们是如何工作的,我的只是不起作用;似乎不起作用。

Below, I've rigged it to pass some made up data, in 在下面,我将其操纵以传递一些组合数据,

            (function () {
                'use strict';
                angular.module('MPAapp')
                    .controller('workCentreCtrl',
                                ['$scope', '$rootScope', 'toastrFactory', 'workCentreResource', '$uibModal', '$log',
                                 workCentreCtrl])

                function workCentreCtrl($scope, $rootScope, toastrFactory, workCentreResource, $uibModal, $log) {
                    var scope = this;

                    var slot = [{'slot1':5}, {'slotname':'dynamo'},{'OriginalSlot':5}]
                    var max = 5

// Click event from the view
                    $scope.EditWorkOrder = function () {                                    
                        var modalInstance = $uibModal.open({
                            animation: true,
                            templateUrl: '/app/WorkOrder/Views/EditWorkOrder.html',
                            controller: 'EditWorkOrderCtrl',
                            size: 'lg',
                            resolve: {
                                data:  function () {
                                    return{
                                        Slot: slot,
                                        Max: max
                                    }                    
                                }
                            }
                        });

                        modalInstance.result.then(function () {
                            $log.info('do some UI update here');
                        }, function () {
                            $log.info('Modal dismissed at: ' + new Date());
                        });

                    };
                }

            }());
            /* END PARENT CONTROLLER */

            /* --------------------------------*/

            /* MODAL INSANCE CONTROLLER BEGIN*/

            (function () {
                'use strict';
                angular.module('MPAapp')
                    .controller('EditWorkOrderCtrl', ['$scope', '$timeout', 'toastrFactory',
                        EditWorkOrderCtrl]);

                EditWorkOrderCtrl.$inject = ['$uibModalInstance', 'data']

                function EditWorkOrderCtrl($scope, $timeout, toastrFactory,  $uibModalInstance, data) {
                    var scope = this;

                    $scope.ok = function () {
                        $uibModalInstance.close(scope.Slot);
                    };

                    $scope.cancel = function () {
                        $uibModalInstance.dismiss('cancel');
                    };
                    // THIS IS WHEN DATA IS UNDEFINED.
                    scope.Slot = data.Slot;
                    scope.SlotNumber = data.Slot.OriginalSlot;
                }
            }());

And the HTML in the modal instance 以及模式实例中的HTML

<div ng-controller="EditWorkOrderCtrl as vm">
    <div class="row">
        <div class="col-md-12">
            <h2>Edit Work Order {{vm.Slot.WorkOrderNumber}}</h2>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12"><span class="main-text bold">Product:</span> {{vm.Slot.ProductCode}} -  {{vm.Slot.ProductDescription}}</div>
    </div>
    <div class="row">
        <div class="col-md-6">
            <span class="main-text bold">Size:</span> {{vm.Slot.QuantityRequired}}
            <span class="main-text bold">Time (mins):</span> {{vm.Slot.StandardRunTime}}
            <span class="main-text bold">Current Date:</span>{{vm.Slot.OriginalOrderDate | date:'dd MMM yyyy'}}
        </div>
        <div class="col-md-6"></div>
    </div>
</div>

Any help very much appreciated. 任何帮助,非常感谢。 I'm still quite new to Angular, it's proving a tough nut in some areas, but I love it! 我对Angular还是很陌生,在某些领域证明它是个难题,但我喜欢它!

Of course data is not going to be available like this (but it's not null , it's undefined ). 当然,这样的data将不可用(但它不是null ,它是undefined )。 Your dependency injection is messed up. 您的依赖项注入混乱了。 Note, that what you are describing in $inject array, must correspond to formal parameters passed to controller function. 请注意,您在$inject数组中描述的内容必须与传递给控制器​​函数的形式参数相对应。

In your case with this configuration: 在这种情况下,您需要:

EditWorkOrderCtrl.$inject = ['$uibModalInstance', 'data']
function EditWorkOrderCtrl($scope, $timeout, toastrFactory,  $uibModalInstance, data) {}

you tell Angular to inject $uibModalInstance as $scope , and data as $timeout . 您告诉Angular将$uibModalInstance注入为$scope ,并将data注入为$timeout Clearly not what you want. 显然不是您想要的。

Correct injection should look like 正确注射应该像

EditWorkOrderCtrl.$inject = ['$scope', '$timeout', 'toastrFactory',  '$uibModalInstance', 'data'];
function EditWorkOrderCtrl($scope, $timeout, toastrFactory,  $uibModalInstance, data) {}

Alternatively you can use array notation as controller definition: 另外,您可以使用数组符号作为控制器定义:

.controller('EditWorkOrderCtrl', ['$scope', '$timeout', 'toastrFactory', 'workCentreResource', 'blockedDatesResource', 'data', EditWorkOrderCtrl]); 

but in this case make sure you don't use EditWorkOrderCtrl.$inject = ['$uibModalInstance'] . 但在这种情况下,请确保您不使用EditWorkOrderCtrl.$inject = ['$uibModalInstance'] Remove it because it has higher priority and as I explained above it's messed up. 删除它是因为它具有更高的优先级,并且正如我上面所解释的那样,将其弄乱了。

Also take a look at this answer , where I provided detail explanation about different injection methods. 还要看一下这个答案 ,在这里我提供了有关不同注射方法的详细说明。

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

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