Reputation: 1075
I have an input and a button inside a form.
I want to type in the input and hit enter, submitting the input content and triggering a modal to open, displaying the results. I don't have any idea how to trigger a modal on ng-submit without getting custom/hacky. Is there an understood method for doing it?
Currently:
<form>
<div ng-show="thirdPhase" class="input-group animated fadeIn">
<input type="text" class="form-control finderBar" placeholder="City, State..." autofocus>
<span class="input-group-btn">
<button data-toggle="modal" data-target="#searchModal" class="btn btn-default" type="button">Go!</button>
</span>
</div>
</form>
And Modal:
<div class="modal fade" id="searchModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<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="myModalLabel">Search Results</h4>
</div>
<div class="modal-body">
TEST SEARCH MODAL
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
I've tried putting the typical modal trigger attributes in the form element, but that simply triggers the modal as soon as you click on the input inside the form.
Upvotes: 0
Views: 2183
Reputation: 8980
As mentioned in the comments, it's the best to use angular-ui-bootstrap
.
Then you can create a factory for creating your modal and you can open the result modal from a method of your first modalInstanceController
.
The factory in the demo is very basic and could be improved e.g. passing the controller name with the params to avoid the hard-coded controller name.
Please have a look at the demo below or in this jsfiddle.
angular.module('ui.bootstrap.demo', ['ui.bootstrap'])
.factory('modalFactory', function($uibModal) {
return {
open: function(size, template, params) {
return $uibModal.open({
animation: true,
templateUrl: template || 'myModalContent.html',
controller: 'ModalResultInstanceCtrl',
size: size,
resolve: {
params: function() {
return params;
}
}
});
}
};
})
.controller('ModalDemoCtrl', function($rootScope, $scope, $log, $uibModal) {
//$scope.items = ['item1', 'item2', 'item3'];
//$scope.animationsEnabled = true;
$scope.open = function(size, template) {
var modalInstance = $uibModal.open({
animation: $scope.animationsEnabled,
templateUrl: template || 'myModalContent.html',
controller: 'ModalInstanceCtrl',
size: size
});
/*modalInstance.result.then(function(selectedItem) {
$scope.selected = selectedItem;
}, function() {
$log.info('Modal dismissed at: ' + new Date());
});*/
};
$scope.toggleAnimation = function() {
$scope.animationsEnabled = !$scope.animationsEnabled;
};
});
// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $uibModal service used above.
angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function($scope, $uibModalInstance, modalFactory) {
//$scope.searchTerm = term;
$scope.ok = function() {
modalFactory.open('lg', 'result.html', {searchTerm: $scope.searchTerm});
//$uibModalInstance.close($scope.searchTerm);
};
$scope.cancel = function() {
$uibModalInstance.dismiss('cancel');
};
});
angular.module('ui.bootstrap.demo').controller('ModalResultInstanceCtrl', function($scope, $uibModalInstance, params) {
$scope.searchTerm = params.searchTerm;
$scope.ok = function() {
$uibModalInstance.close($scope.searchTerm);
};
$scope.cancel = function() {
$uibModalInstance.dismiss('cancel');
};
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.14.3/ui-bootstrap-tpls.min.js"></script>
<div ng-app="ui.bootstrap.demo">
<div ng-controller="ModalDemoCtrl">
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3 class="modal-title">I'm a modal!</h3>
</div>
<div class="modal-body">
Demo form submit:
<br/>
<form ng-submit="ok()">
<div class="input-group animated fadeIn">
<input type="text" class="form-control finderBar" ng-model="searchTerm" placeholder="City, State..." autofocus>
<span class="input-group-btn">
<button class="btn btn-default" type="button" ng-click="ok()">Go!</button>
</span>
</div>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-primary" type="button" ng-click="ok()">OK</button>
<button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
</div>
</script>
<script type="text/ng-template" id="result.html">
<div class="modal-header">
<h3 class="modal-title">I'm a modal!</h3>
</div>
<div class="modal-body">
You searched for: {{searchTerm}}
</div>
<div class="modal-footer">
<button class="btn btn-primary" type="button" ng-click="cancel()">Close</button>
</div>
</script>
<button type="button" class="btn btn-default" ng-click="open()">Open me!</button>
<!--<button type="button" class="btn btn-default" ng-click="toggleAnimation()">Toggle Animation ({{ animationsEnabled }})</button>-->
<div ng-show="searchTerm">searchTerm from a modal: {{ searchTerm }}</div>
</div>
</div>
Upvotes: 1
Reputation: 215
So assuming you dont want a special button for modal to pop up from, and just ngSubmit to lead the way(and without getting hacky), you need to modify your html to something like this,
<div ng-controller="whateverYouWant" data-target="#searchModal" data-toggle="modal">
<form ng-submit="submit()">
<div ng-show="thirdPhase" class="input-group animated fadeIn">
<input type="text" class="form-control
finderBar" placeholder="City, State..." autofocus>
<span class="input-group-btn">
</span>
</div>
</form>
</div>
Notice i removed <button>
from HTML and put data-target
and data-modal
in <div>
that has ng-controller.
Finally modify your script to something like this :
$scope.submit = function (){
.
.
.
$('#searchModal').modal('show');
}
With this ,when the ng-submit finishes its task it automatically initializes the modal and you can use ng-modal
to bind the data.
Checkout Modals.
Upvotes: 1
Reputation: 2899
Here in below example, I have created my own modal directive.
HTML:
<div ng-controller="MainCtrl" class="container">
<h1>Modal example</h1>
<form ng-submit="toggleModal()">
<div class="input-group animated fadeIn">
<input type="text" class="form-control finderBar" placeholder="City, State..." autofocus>
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Go!</button>
</span>
</div>
</form>
<modal title="Search Results" visible="showModal">
TEST SEARCH MODAL
</modal>
</div>
JS:
var mymodal = angular.module('mymodal', []);
mymodal.controller('MainCtrl', function ($scope) {
$scope.showModal = false;
$scope.toggleModal = function(){
$scope.showModal = !$scope.showModal;
};
});
mymodal.directive('modal', function () {
return {
template: '<div class="modal fade">' +
'<div class="modal-dialog">' +
'<div class="modal-content">' +
'<div class="modal-header">' +
'<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>' +
'<h4 class="modal-title">{{ title }}</h4>' +
'</div>' +
'<div class="modal-body" ng-transclude></div>' +
'</div>' +
'</div>' +
'</div>',
restrict: 'E',
transclude: true,
replace:true,
scope:true,
link: function postLink(scope, element, attrs) {
scope.title = attrs.title;
scope.$watch(attrs.visible, function(value){
if(value == true)
$(element).modal('show');
else
$(element).modal('hide');
});
$(element).on('shown.bs.modal', function(){
scope.$apply(function(){
scope.$parent[attrs.visible] = true;
});
});
$(element).on('hidden.bs.modal', function(){
scope.$apply(function(){
scope.$parent[attrs.visible] = false;
});
});
}
};
});
You can use ui-bootstrap
as well as @Pankaj Parkar suggested.
For more information https://angular-ui.github.io/bootstrap/#/modal
Upvotes: 1