我如何在AngularJS Bootstrap模式中不使用$ scope?

时间:2015-09-29 15:08:23

标签: angularjs angular-bootstrap

我在控制器中有这个代码。我用它来打开一个带有另一个控制器的angularJS Bootstrap模态。

有没有办法在这里使用$ scope作为ok()和cancel()函数?

   $scope.deleteTest = (test: ITest) => {
        var self = this;
        var modalInstance = $modal.open({
            templateUrl: '/app/tests/partials/deleteTest.html',
            controller: ['$scope', '$modalInstance', 'testService',
                function ($scope, $modalInstance, testService: ITestService) {
                    $scope.ok = () => {
                        var self = this;
                        testService.deleteTest()
                            .then(
                                () => {
                                    $modalInstance.close();
                                });
                    };
                    $scope.cancel = () => {
                        $modalInstance.dismiss('cancel');
                    };
                }],
        });
    }

1 个答案:

答案 0 :(得分:0)

看起来你正在使用TypeScript,所以你可以做这样的事情

var modalInstance = $modal.open({
    templateUrl: '/app/tests/partials/deleteTest.html',
    controller: DeleteTest,
    controllerAs: 'ctrl'
});

//...

class DeleteTest {
    static $inject = ['$modalInstance', 'testService'];

    constructor(private $modalInstance, private testService: ITestService) {

    }

    ok() {
        this.testService.deleteTest().then(() => this.$modalInstance.close());
    }

    cancel() {
        this.$modalInstance.dismiss('cancel');
    }

}

deleteTest.html中,您可以使用类似

的方式调用ok()方法
<button ng-click="ctrl.ok()">Ok</button>