我在控制器中有这个代码。我用它来打开一个带有另一个控制器的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');
};
}],
});
}
答案 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>