我怎么能不在 AngularJS Bootstrap 模式中使用 $scope?
How can I not use $scope in an AngularJS Bootstrap modal?
我在控制器中有这段代码。我正在使用它来打开一个 angularJS Bootstrap 模式,并附加了另一个控制器。
有什么方法可以让我在此处的 ok() 和 cancel() 函数中不使用 $scope 吗?
$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');
};
}],
});
}
看起来你正在使用 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>
我在控制器中有这段代码。我正在使用它来打开一个 angularJS Bootstrap 模式,并附加了另一个控制器。
有什么方法可以让我在此处的 ok() 和 cancel() 函数中不使用 $scope 吗?
$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');
};
}],
});
}
看起来你正在使用 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>