使用模式从父作用域调用函数 (angular)
using a modal to invoke a function from the parent scope (angular)
我有一个 main.controller.js,在那个控制器中,我有一个模态,我想用它来通知用户警告,然后再继续应用程序的其余部分。
例如...我想从模态
调用我的 fileNew 函数
main.controller.js:
$scope.warningModal = function() {
$modal.open({
animation: true,
templateUrl: "./modal.html",
controller: "modalController",
});
};
$scope.fileNew = function(type, subject, application) {
newApplication(type, subject, application)
$state.go('submissions');
});
};
main.html:
<li><a ng-click="warningModal()">New app</a></li>
modal.html:
<div class="modal-body">
<p>warning message</p>
</div>
<div class="modal-footer clearfix">
<button class="pull-right btn btn-primary" ng-click="$parent.fileNew()">Continue</button>
</div>
模态控制器:
$scope.$parent.fileNew();
在控制台中给我一条警告消息说..$scope.$parent.fileNew 不是函数。
有什么想法吗?
编辑:
关于这个想法的另一个快速问题...
我有 3 种不同类型的 fileNew 申请要提交...
所以在我的主要 controller.js:
$scope.fileNew = function(type, subject, application) {
newApplication(type, subject, application)
$state.go('submissions');
});
};
$scope.fileOld = function(type, subject, application) {
newApplication(type, subject, application)
$state.go('submissions');
});
};
$scope.fileEdit = function(type, subject, application) {
newApplication(type, subject, application)
$state.go('submissions');
});
};
有什么方法可以让我通过在模态框上点击同一个继续按钮吗?
在 Angular UI Bootstrap 的最新版本中,您可以包含一个 scope
选项,您可以在其中传递您希望在模态控制器中可用的范围:
$scope.warningModal = function() {
$modal.open({
animation: true,
scope: $scope,
templateUrl: "./modal.html",
controller: "modalController",
});
};
所以现在您可以在 modalController
:
中调用您的函数
$scope.fileNew(type, subject, application);
我有一个 main.controller.js,在那个控制器中,我有一个模态,我想用它来通知用户警告,然后再继续应用程序的其余部分。
例如...我想从模态
调用我的 fileNew 函数main.controller.js:
$scope.warningModal = function() {
$modal.open({
animation: true,
templateUrl: "./modal.html",
controller: "modalController",
});
};
$scope.fileNew = function(type, subject, application) {
newApplication(type, subject, application)
$state.go('submissions');
});
};
main.html:
<li><a ng-click="warningModal()">New app</a></li>
modal.html:
<div class="modal-body">
<p>warning message</p>
</div>
<div class="modal-footer clearfix">
<button class="pull-right btn btn-primary" ng-click="$parent.fileNew()">Continue</button>
</div>
模态控制器:
$scope.$parent.fileNew();
在控制台中给我一条警告消息说..$scope.$parent.fileNew 不是函数。
有什么想法吗?
编辑:
关于这个想法的另一个快速问题...
我有 3 种不同类型的 fileNew 申请要提交...
所以在我的主要 controller.js:
$scope.fileNew = function(type, subject, application) {
newApplication(type, subject, application)
$state.go('submissions');
});
};
$scope.fileOld = function(type, subject, application) {
newApplication(type, subject, application)
$state.go('submissions');
});
};
$scope.fileEdit = function(type, subject, application) {
newApplication(type, subject, application)
$state.go('submissions');
});
};
有什么方法可以让我通过在模态框上点击同一个继续按钮吗?
在 Angular UI Bootstrap 的最新版本中,您可以包含一个 scope
选项,您可以在其中传递您希望在模态控制器中可用的范围:
$scope.warningModal = function() {
$modal.open({
animation: true,
scope: $scope,
templateUrl: "./modal.html",
controller: "modalController",
});
};
所以现在您可以在 modalController
:
$scope.fileNew(type, subject, application);