我有一个 main.controller.js,在该 Controller 中,我有一个模式,我想用它来通知用户警告,然后再继续应用程序的其余部分。
例如...我想从模式中调用我的 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>
模态 Controller :
$scope.$parent.fileNew();
在控制台中给我一条警告消息,说..$scope.$parent.fileNew 不是一个函数。
有什么想法吗?
编辑:
围绕这个想法还有一个简单的问题......
我有 3 种不同类型的文件我想要提交的新应用程序...
所以在我的主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
选项,您可以在其中传递您希望在模态 Controller 中可用的范围:
$scope.warningModal = function() {
$modal.open({
animation: true,
scope: $scope,
templateUrl: "./modal.html",
controller: "modalController",
});
};
现在您可以从 modalController
中调用您的函数:
$scope.fileNew(type, subject, application);
关于javascript - 使用模态从父作用域调用函数( Angular ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43854382/