$uibModal close($value) 回调总是 returns undefined

$uibModal close($value) callback always returns undefined

在我的模板中,我使用 ng-click="$widget.addItem()" 触发 $uibModal

一切正常,打开、关闭等。

问题是,我无法使 $modal.close($value) 回调值起作用。函数本身工作正常,模式关闭,调用 promise 成功函数。问题是,它总是 returns undefined.


组件控制器

app.component('list', {
    templateUrl: 'widgets/list.html',
    controllerAs: '$widget',
    controller: function($uibModal) {

        // Add item through modal
        this.addItem = function() {

            // Init and open $uibModal
            var modalInstance = $uibModal.open({
                component   : 'modalAddItem',
                size        : 'md'
            });

            // $uibModal promise
            modalInstance.result.then(function(params) {
                console.log(params)
                // ---> undefined 
            });
        }
    }
}

模态模板

<div class="modal-add-item">

    <div class="modal-body" id="modal-body">
        // Some template stuff here 
    </div>

    <div class="modal-footer">
        <button class="btn btn-sm btn-default" ng-click="$modal.ok()">Ok</button>
    </div>

</div>

模态组件

app.component('modalAddItem', {
    templateUrl: 'widgets/modals/add-item.html',
    bindings: {
        resolve: '<',
        close: '&',
        dismiss: '&'
    },
    controllerAs: '$modal',
    controller: function ($scope) {

        this.ok = function() {
            this.close({item: 'picked item'});
        }

    }
});

一切正常。但是无论我尝试什么,关闭函数总是 returns undefined

你应该使用 $value 而不是项目。

this.ok = function() {
  this.close({$value: 'picked item'});
}

close - A method that can be used to close a modal, passing a result. The result must be passed in this format: {$value: myResult}

Angular UI bootstrap