How to use angular-ui-bootstrap (modals) in typescript?

前端 未结 2 1860
别跟我提以往
别跟我提以往 2021-02-08 11:03

I\'d like to edit some data from a table using a modal. There are various interfaces in the typescript definitions for angular-ui-bootstrap from definitelyTyped, however they ar

2条回答
  •  有刺的猬
    2021-02-08 11:24

    The instance injected by angular will be of type ng.ui.bootstrap.IModalService.

    And since you are using "controller as" syntax, there is no need to start using $scope here, instead you can use resolve as shown in the angular-ui modal example.

    Here's the example code:

    class ItemsListController {
        static controllerId = 'ItemsListController';
        static $inject = ['$modal'];
    
        data = [
            { value1: 'Item1Value1', value2: 'Item1Value2' },
            { value1: 'Item2Value1', value2: 'Item2Value2' }
        ];
    
        constructor(private $modal: ng.ui.bootstrap.IModalService) { }
    
        edit(item) {
            var options: ng.ui.bootstrap.IModalSettings = {
                templateUrl: 'modal.html',
                controller: ItemDetailController.controllerId + ' as modal',
                resolve: {
                    item: () => item // <- this will pass the same instance 
                                     // of the item displayed in the table to the modal
                }
            };
    
            this.$modal.open(options).result
                .then(updatedItem => this.save(updatedItem));
                //.then(_ => this.save(item)); // <- this works the same way as the line above
        }
    
        save(item) {
            console.log('saving', item);
        }
    }
    
    class ItemDetailController {
        static controllerId = 'ItemDetailController';
        static $inject = ['$modalInstance', 'item'];
    
        constructor(private $modalInstance: ng.ui.bootstrap.IModalServiceInstance, public item) { }
    
        save() {
            this.$modalInstance.close(this.item); // passing this item back 
                                                  // is not necessary since it 
                                                  // is the same instance of the 
                                                  // item sent to the modal
        }
    
        cancel() {
            this.$modalInstance.dismiss('cancel');
        }
    }
    

提交回复
热议问题