如何在打字稿中使用angular-ui-bootstrap(modals)?

xvd*_*iff 10 javascript angularjs typescript angular-ui angular-ui-bootstrap

我想使用模态从表中编辑一些数据.来自definitelyTyped的angular-ui-bootstrap的typescript定义中有各种接口,但它们没有文档,我无法找到有关如何使用它们的任何示例.

  • IModalScope
  • IModalService
  • IModalServiceInstance
  • IModalSettings
  • IModalStackService

https://github.com/borisyankov/DefinitelyTyped/blob/master/angular-ui-bootstrap/angular-ui-bootstrap.d.ts#L146

我想要实现的是这样的:

布局

我是否正确地假设ItemsListController和ItemDetailModalController都需要一个相同范围的实例才能交换数据?如何使用上面的接口为模态指令定义控制器和模板?

我已经在这里找到了这个非打字稿的例子:https://angular-ui.github.io/bootstrap/#/modal

然而,作为初学者,我很难理解如果样本将所有内容都放在一个文件中而不分离问题的话会发生什么.

Jua*_*njo 21

通过angular注入的实例将是类型ng.ui.bootstrap.IModalService.

由于您使用的是"控制器为"语法,因此无需在$scope此处开始使用,而是可以使用angular-ui模式示例中所示的resolve .

这是示例代码:

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');
    }
}
Run Code Online (Sandbox Code Playgroud)


bas*_*rat 1

我是否可以假设 ItemsListController 和 ItemDetailModalController 都需要相同范围的实例才能交换数据?

是的。我实际上认为模态是ItemsListController包含成员的扩展shownDetails:ItemDetailModalController。这意味着您不需要想出一种混乱的共享方式,$scope因为它只是一个$scope.

如何使用上面的接口定义控制器和模态指令的模板?

这就是我所拥有的(请注意,您正在共享范围):

            this.$modal.open({
                templateUrl: 'path/To/ItemModalDetails.html',
                scope: this.$scope,
            })
Run Code Online (Sandbox Code Playgroud)

哪里$modal:IModalService对应于角度带给你的: http: //angular-ui.github.io/bootstrap/#/modal