我有一个带有动态标题和消息的模态,我还想在测试它时为它提供一个动态函数;函数在显示模态之前被触发,是否有办法不触发该函数,而是将其放入范围var中,以便可以在可重用模式中使用?
动态模式:
<div class="modal fade" id="confirmationModal-reusable" tabindex="-1" role="dialog" aria-labelledby="confirmationTitle2">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="confirmationTitle2">{{reusableTitle}}</h4>
</div>
<div class="modal-body">
<p>{{reusableMessage}}</p>
</div>
<div class="modal-footer bootstrap-iso">
<button type="button" ng-click="{{reusableFunction}}" class="btn btn-danger" data-dismiss="modal">Yes</button>
<button type="button" class="btn btn-success" data-dismiss="modal">No</button>
</div>
</div>
</div>
</div>
JavaScript的:
$scope.Refresh = function(){
//prompt for refresh, all unsaved items will be lost.
$scope.reusableTitle = "Are you sure?";
$scope.reusableMessage = "Are you sure you want to abandon changes and refresh from server?";
$scope.reusableFunction = $scope.init();//how can I pass this function to the ng-click on my reusable modal?
$("#confirmationModal-reusable").modal('show');
};
$scope.init = function() {
//gets json from server...
};
答案 0 :(得分:1)
你可以这样:
<button ng-click="reusableFunction('init')">Click this button</button>
然后在控制器中执行以下操作:
$scope.reusableFunction = function (functionName){
if(angular.isFunction($scope[functionName])){
$scope[functionName]();
}
}
$scope.init = function(){
//do your stuff
}
以下是isFunction
来电https://docs.angularjs.org/api/ng/function/angular.isFunction
答案 1 :(得分:0)
我认为你唯一需要做的就是这个
<button type="button" ng-click="reusableFunction()" class="btn btn-danger" data-dismiss="modal">Yes</button>