I'm just finding my way with Angular, and more importantly trying to find my way without jQuery!
I'd like to have a view that shows a loading spinner while data is fetched from the server, and when it arrives (the model will have a property of "Populated") I want the spinner to fade out, and the content to fade in.
I'm using a directive for the loading bit, and ng-show seems simple enough to switch the sections in the view.
View:
<div ng-hide="model.Populated" loading-spinner></div>
<div ng-show="model.Populated"><!-- Content goes here --> </div>
Directive:
module App.Directives {
declare var Spinner: any;
export class LoadingSpinner {
constructor() {
var directive: ng.IDirective = {};
directive.restrict = 'A';
directive.scope = { loading: '=myLoadingSpinner'},
directive.template = '<div class="loading-spinner-container"></div>';
directive.link = function (scope, element, attrs) {
var spinner = new Spinner().spin();
var loadingContainer = element.find('.loading-spinner-container')[0];
loadingContainer.appendChild(spinner.el);
};
return directive;
}
}
It's the animation I'm not sure about. In particular, I want the content to fade in once the spinner has completely faded out, and I'm not sure how to do this with a callback.
Should I attempt all the animation with CSS or expand on my directive and use javascript?
(I'm using TypeScript for anyone wondering about the syntax)
I did a quick spike for my app yesterday and this is how it can be easily done.
This uses ui.bootstrap modal dialog.
When you have a long running process such as remote service call you "raise" an event via $emit. This will bubble up to your outer most scope. Here is a sample from typeahead search functionality I spiked it against.
function autoCompleteClientName(searchValue, searchType) {
var self = this;
self.$emit("WorkStarted", "Loading Clients...");
//return promise;
if (searchType === 'name') {
return $scope.clientSearchDataService.getClientsByName(searchValue)
.then(function (response) {
self.$emit("WorkCompleted", "");
return response.results;
}, function(error) {
self.$emit("WorkCompleted", "");
console.warn("Error: " + error);
});
} else if (searchType === 'number') {
return $scope.clientSearchDataService.getClientsByNumber(searchValue)
.then(function (response) {
self.$emit("WorkCompleted", "");;
return response.results;
}, function (error) {
self.$emit("WorkCompleted", "");
console.warn("Error: " + error);
});
}
};
Then we have a "shell" controller that is the controller for outermost view, the one that hosts ng-view.
(function () {
'use strict';
// Controller name is handy for logging
var controllerId = 'shellCtrl';
// Define the controller on the module.
// Inject the dependencies.
// Point to the controller definition function.
angular.module('app').controller(controllerId,
['$scope', '$modal', shellCtrl]);
function shellCtrl($scope,$modal) {
// Bindable properties and functions are placed on vm.
$scope.title = 'shellCtrl';
$scope.$on("WorkStarted", function(event, message) {
$scope.modalInstance = $modal.open({ templateUrl: "app/common/busyModal/busyModal.html" });
});
$scope.$on("WorkCompleted", function (event, message) {
$scope.modalInstance.close();
});
}
})();
Here is the modal template
<div class="modal-dialog">
<div class="modal-content">
<img src="/images/loading.gif"width="55" height="55"/>
<h3>Loading data...</h3>
</div>
<!-- /.modal-content -->
</div><!-- /.modal-dialog -->
For this to appear you have to override some styles
<style>
.modal
{
display: block;
}
.modal-body:before,
.modal-body:after
{
display: table;
content: " ";
}
.modal-header:before,
.modal-header:after
{
display: table;
content: " ";
}
</style>
and if you need full template for modal
<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">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
Keep in mind that this is just a spike that took me about 30 min to wire together. For more robust solution, you need to be able to keep track of number of processes started and completed etc, if you are executing multiple calls to remove service.
Related
Our team is developing an attachment widget in ServiceNow. The widget allows a user to attach documents, view them, and delete them as necessary. In order to view the attachments, we're leveraging modal windows through uibModal. If a user deletes all of the attachments and the array length reaches zero, we want the modal window to close automatically, but we can't seem to get that working.
<label>
<sp-attachment-button></sp-attachment-button>
</label>
<span ng-if="attachments.length>0" class="badge" ng-click="c.openModal()">{{attachments.length}}</span>
<script type="text/ng-template" id="modalTemplate">
<div class="panel panel-default">
<div class="panel-heading flex">
<h4 class="panel-title">Attachments</h4>
<i type="button" class="fa fa-times" style="margin-left:auto;" ng-click="c.closeModal()"></i>
</div>
<div class="panel-body">
<now-attachments-list template="sp_attachment_single_line"></now-attachments-list>
</div>
<!--<div class="panel-footer text-right">
<button class="btn btn-primary" ng-click="c.closeModal()">${Close Modal}</button>
</div>-->
</div>
</script>
Our controller looks like this:
function ($uibModal, spModal, cabrillo, $scope, $http, spUtil, nowAttachmentHandler, $rootScope, $sanitize, $window, $sce) {
var c = this;
$scope.attachments=[];
$scope.m = $scope.data.msgs;
$scope.submitButtonMsg = $scope.m.submitMsg;
if(c.options.case_sysid){
$scope.data._attachmentGUID = c.options.case_sysid;
}
var ah = $scope.attachmentHandler = new nowAttachmentHandler(setAttachments, function() {});
ah.setParams('sn_hr_core_case_workforce_admin', $scope.data._attachmentGUID, 1024 * 1024 * 24);
function setAttachments(attachments, action) {
$scope.attachments = attachments;
}
$scope.attachmentHandler.getAttachmentList();
$scope.confirmDeleteAttachment = function(attachment) {
if (cabrillo.isNative()) {
if (confirm($scope.data.msgs.delete_attachment)) {
$scope.attachmentHandler.deleteAttachment(attachment);
}
} else {
spModal.confirm($scope.data.msgs.delete_attachment).then(function() {
$scope.attachmentHandler.deleteAttachment(attachment);
if($scope.attachments.length==0){
c.modalInstance.close();
}
});
}
}
c.openModal = function() {
c.modalInstance = $uibModal.open({
templateUrl: 'modalTemplate',
scope: $scope
});
}
c.closeModal = function() {
c.modalInstance.close();
}
console.log('custom-attachments');
console.log($scope);
}
Any suggestions on how to get the modal to close automatically?
We figured it out. We have to watch the array, then close the modal after array hits zero:
$scope.$watch(function () {
return $scope.attachments;
}, function (value) {
if(value.length==0){
if(c.modalInstance){
c.modalInstance.close();
}
}
});
I am trying to create a service to display Angular-Strap modals since I have many different parts of code that will need to trigger a modal and I don't want to run into a situation where I would have a circular reference.
This is code that works where you have access to $scope. For instance, in the applications controller.
function MyModalController($scope) {
$scope.title = 'Draw Object Properties';
$scope.content = 'Hello Modal<br />This is place holder test';
};
MyModalController.$inject = ['$scope'];
// Pre-fetch an external template populated with a custom scope
var myOtherModal = $modal({ controller: MyModalController, templateUrl: 'webmapapi/modal/toolproperties.html', show: false });
// Show when some event occurs (use $promise property to ensure the template has been loaded)
$scope.showModal = function () {
myOtherModal.$promise.then(myOtherModal.show);
};
Like I said I need to call this from a service though.
(function (angular, undefined) {
'use strict';
angular.module('ModalService', ['service', 'webValues', 'msObjects', 'mgcrea.ngStrap',])
.config(function ($modalProvider) {
angular.extend($modalProvider.defaults, {
html: true
});
})
.factory("ModalService", function (MapApiService, webValues, VectorObjs,$modal) {
var modalSVC = {
};
modalSVC.showModal = function (modalType) {
var scope = angular.element(document.getElementById('mainContainer')).scope();
function MyModalController(scope) {
scope.title = 'Draw Object Properties';
scope.content = 'Hello Modal<br />This is place holder test';
};
MyModalController.$inject = ['scope'];
// Pre-fetch an external template populated with a custom scope
var myOtherModal = $modal({ controller: MyModalController, templateUrl: 'myURL.html', show: true });
// Show when some event occurs (use $promise property to ensure the template has been loaded)
myOtherModal.show;
};
return modalSVC;
})
}(angular));
The above does not like the scope I'm getting.
Okay, It's amazing how easy something can be once you know what you are doing!!
Essentially you will want to set up a service...
(function (angular, undefined) {
'use strict';
angular.module('ModalService', ['mgcrea.ngStrap'])
.controller('ModalServiceController', modalServiceController)
.factory("ModalService", function ($animate, $document, $compile, $controller, $http, $rootScope, $q, $templateRequest, $timeout, $modal) {
function ModalService() {
var self = this;
self.showModal = function (title,templateUrl) {
var modal = $modal({
title: title,
templateUrl: templateUrl,
show: true,
backdrop: 'static',
controller: 'ModalServiceController'
});
modal.show;
};
}
return new ModalService();
})
modalServiceController.$inject = ['$scope', '$modal'];
function modalServiceController($scope,$modal) {
//title and content need to be populated so I just left the default values
$scope.title = 'Draw Object Properties';
$scope.content = 'Hello Modal<br />This is place holder test';
};
}(angular));
Now that you have your controller set up and injecting $modal, all you have to do from anywhere you have your service reference injected is...
ModalService.showModal("Your Title",
"Your URL");
I have a template(must be formatted as ) set up as Template.html and the contents are...
<div class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header" ng-show="title">
<button type="button" class="close" ng-click="$hide()">×</button>
<h4 class="modal-title" ng-bind-html="title"></h4>
</div>
<div class="modal-body" ng-show="content">
<h4>Text in a modal</h4>
<p ng-bind-html="content"></p>
<pre>2 + 3 = {{ 2 + 3 }}</pre>
<h4>Popover in a modal</h4>
<p>This <a href="#" role="button" class="btn btn-default popover-test" data-title="A Title" data-content="And here's some amazing content. It's very engaging. right?" bs-popover>button</a> should trigger a popover on click.</p>
<h4>Tooltips in a modal</h4>
<p><a href="#" class="tooltip-test" data-title="Tooltip" bs-tooltip>This link</a> and <a href="#" class="tooltip-test" data-title="Tooltip" bs-tooltip>that link</a> should have tooltips on hover.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" ng-click="$hide()">Close</button>
<button type="button" class="btn btn-primary" ng-click="$hide()">Save changes</button>
</div>
</div>
</div>
I hope this helps you!!
As I am a newbie in angular with typescript I am facing a issue while implemented angular modal popup. The issue is I have one drop-down on which change I have to open a modal popup and that modal popup will have two buttons "Yes" or "No". For this I have one controller where I have injected a dependency.
export class QuestionnaireController {
static ngControllerName = 'questionnaireController';
static inject = ["$uibModal"];
constructor(private $uibModal: ng.ui.bootstrap.IModalService) {
}
public openModalPopup() {
let options: ng.ui.bootstrap.IModalSettings = {
controller: QuestionnaireController,
controllerAs:'ctrl',
templateUrl: 'app/views/Dialogbox.html',
};
this.$uibModal.open(options);
}
}
Most of my code is written in 'QuestionnaireController' and the popup is getting open using this controller but I also want to close this popup so I read a article where it was written that I have to created a new controller "ModalController " to make popup close.
export class ModalController {
static inject = ["$uibModalInstance"];
constructor(private $uibModalInstance: ng.ui.bootstrap.IModalServiceInstance) {
}
public close() {
this.$uibModalInstance.close();
}
}
Popup code is here...
<div ng-app="" id="dvModal">
<div class="modal-header">
</div>
<div class="modal-body">
<p> Evaluated result will be discarded if you continue. Are you sure you want to continue?</p>
</div>
<div class="modal-footer">
<input id="yesBtn" type="button" class="btn btn-default" ng-click="ctrl.Yes('true')" value="Yes" />
<input id="npBtn" type="button" class="btn btn-default" ng-click="ctrl.close()" value="No" />
</div>
and to close this passed Controller : ModalController in options which makes my popup closed on click of "No". But now the issue is generated here, how I again went to "QuestionnaireController" to do "Yes" functionality as "Yes" functionality is written in QuestionnaireController.
Yes, you can!
$uibModal is super flexible tool.
I'm not super familiar with Typescript, but here's my JS solution:
angular
.module('appName', ['ui.bootstrap'])
.controller('SomePageController', ['$scope', '$uibModal', '$log',
function ($scope, $uibModal, $log) {
First you want to do, is to change your openModalPopup() method:
// Instantiate the modal window
var modalPopup = function () {
return $scope.modalInstance = $uibModal.open({
templateUrl: 'blocks/modal/dialog.html',
scope: $scope
});
};
// Modal window popup trigger
$scope.openModalPopup = function () {
modalPopup().result
.then(function (data) {
$scope.handleSuccess(data);
})
.then(null, function (reason) {
$scope.handleDismiss(reason);
});
};
// Close the modal if Yes button click
$scope.yes = function () {
$scope.modalInstance.close('Yes Button Clicked')
};
// Dismiss the modal if No button click
$scope.no = function () {
$scope.modalInstance.dismiss('No Button Clicked')
};
// Log Success message
$scope.handleSuccess = function (data) {
$log.info('Modal closed: ' + data);
};
// Log Dismiss message
$scope.handleDismiss = function (reason) {
$log.info('Modal dismissed: ' + reason);
}
}
]);
Second - modal window HTML template will look like this:
<script type="text/ng-template" id="blocks/modal/dialog.html">
<div class="modal-header">
<h3 class="modal-title">I'm a modal!</h3>
</div>
<div class="modal-body">
Modal content
</div>
<div class="modal-footer">
<button class="btn btn-primary" type="button" ng-click="yes()">Yes</button>
<button class="btn btn-warning" type="button" ng-click="no()">No</button>
</div>
</script>
Third - pretty simple SomePage HTML (in your case - Questionnaire) View example :
<div ng-controller="SomePageController">
<button type="button" class="btn btn-default" ng-click="openModalPopup()">Open modal</button>
</div>
All together:
angular
.module('appName', ['ui.bootstrap'])
.controller('SomePageController', ['$scope', '$uibModal', '$log',
function($scope, $uibModal, $log) {
$scope.modalPopup = function() {
modal = $uibModal.open({
templateUrl: 'blocks/modal/dialog.html',
scope: $scope
});
$scope.modalInstance = modal;
return modal.result
};
$scope.modalPopupTrigger = function() {
$scope.modalPopup()
.then(function(data) {
$scope.handleSuccess(data);
},function(reason) {
$scope.handleDismiss(reason);
});
};
$scope.yes = function() {
$scope.modalInstance.close('Yes Button Clicked')
};
$scope.no = function() {
$scope.modalInstance.dismiss('No Button Clicked')
};
$scope.handleSuccess = function(data) {
$log.info('Modal closed: ' + data);
};
$scope.handleDismiss = function(reason) {
$log.info('Modal dismissed: ' + reason);
}
}
]);
<!DOCTYPE html>
<html>
<head>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body ng-app="appName">
<div ng-controller="SomePageController">
<script type="text/ng-template" id="blocks/modal/dialog.html">
<div class="modal-header">
<h3 class="modal-title">I'm a modal!</h3>
</div>
<div class="modal-body">
Modal content
</div>
<div class="modal-footer">
<button class="btn btn-primary" type="button" ng-click="yes()">Yes</button>
<button class="btn btn-warning" type="button" ng-click="no()">No</button>
</div>
</script>
<button type="button" class="btn btn-default" ng-click="modalPopupTrigger()">Open modal</button>
</div>
<script src="https://code.angularjs.org/1.5.7/angular.min.js"></script>
<script src="https://code.angularjs.org/1.5.7/angular-animate.min.js"></script>
<script src="https://raw.githubusercontent.com/angular-ui/bootstrap-bower/master/ui-bootstrap-tpls.min.js"></script>
</body>
</html>
Well, if you are that lazy guy like me, the following will also work ;)
var objects = [{
name: "obj1",
value: 1
}, {
name: "obj2",
value: 2
}];
// Generating the modal html
var html = "<div class='modal-header'><h4 class='modal-title'>Select Object</h4></div>";
html += "<div class='modal-body'>";
html += "<select class='form-control' ng-model='selection'>";
for (var i = 0; i < objects.length; i++) {
var ob = objects[i];
html += "<option value='" + ob.value + "'>" + ob.name + "</option>";
}
html += "</select>";
html += "</div>";
html += "<div class='modal-footer'>";
html += '<button type="button" ng-click="dismissModal()" class="btn btn-default" >Close</button>';
html += '<button type="button" ng-click="closeModal()" class="btn btn-primary">Select</button>';
html += "</div>";
// Showing the modal
var objectSelectionModal = $uibModal.open({
template: html,
controller: function($scope) {
// The function that is called for modal closing (positive button)
$scope.closeModal = function() {
//Closing the model with result
objectSelectionModal.close($scope.selection);
};
//The function that is called for modal dismissal(negative button)
$scope.dismissModal = function() {
objectSelectionModal.dismiss();
};
}
});
//Processing the Result
objectSelectionModal.result.then(function(selected) {
alert(selected);
});
I am using html5, angularjs, bootstrap. So each one of them gives many options to create a modal window. Whole of my website has 2 things, one is confirmation dialog or form dialog. So which is the best reusable approach. Should i use html5 or create a directive ?
As mentioned in the comments ui-bootstrap Modal is very useful.
Just for an example here is a factory for confirm modal that you can extend according to your use case:
yourApp.factory("dialog", ["$modal",
function($modal) {
var dialogService = {
confirm: function(options) {
var modalInstance = $modal.open({
templateUrl: 'partials/confirm-modal.html',
controller: ['$scope',
function($scope) {
$scope.header = options.header;
$scope.body = options.body;
$scope.confirmText = options.confirmText || "Close";
$scope.cancelText = options.cancelText || "Confirm";
$scope.hideCancelButton = options.hideCancelButton;
$scope.cancel = function() {
modalInstance.dismiss('cancel');
};
$scope.confirm = function() {
modalInstance.close();
};
}
]
});
return modalInstance.result;
}
}
}
])
And here is your template:
<div class="modal-header">
<button type="button" data-dismiss="modal" aria-hidden="true" class="btn close" ng-click="cancel()">×</button>
<h4 id="noteLabel" class="modal-title">{{header}}</h4>
</div>
<div class="modal-body">
<p ng-bind-html="body" style="font-size: 16px"></p>
</div>
<div class="modal-footer">
<button ng-show="!hideCancelButton" class="btn btn-default" ng-click="cancel()">{{cancelText}}</button>
<button ng-click="confirm()" class="btn btn-primary">{{confirmText}}</button>
</div>
My goal is to have a modal view with couple of buttons and a ui-view. When clicking one of those buttons it will load the specific view + controller.
Reading from the angular-ui router project (https://github.com/angular-ui/ui-router/wiki) they control ui-views with states. How could I do that with a modal?
Below you can get a better idea of what i am aiming for. I tried also using states (abstract state to load layout template with buttons leaving a ui-view directive) but it didnt work. Any ideas on how to organize the code to accomplish it?
This is the code to launch the modal view:
$scope.launchModalView = function(data){
var modalInstance = $modal.open({
controller: 'coversModalController',
size: 'lg',
templateUrl: 'partials/covers.modal.html',
resolve: {
data: function(){
return data;
}
}
});
modalInstance.result.then(function (data) {
console.log('Got it! Song = ' + JSON.stringify(data));
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
}
Here the coversModalController:
admintoolApp.controller('coversModalController',function($scope, $modalInstance, data){
// Modal functionality
$scope.data = data;
$scope.submit = function () {
$modalInstance.close($scope.data);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
});
Here we have the template "partials/covers.modal.html":
<div class="modal-header">
<h3 class="modal-title">Select one method to choose images</h3>
</div>
<div class="modal-body" style="overflow: hidden;">
<div style="float:left;">
<ul>
<li>From Private Library</li>
<li>From External Resource</li>
<li>Upload image</li>
</ul>
</div>
<div style="float:left;" ui-view>
<h4>Choose one of the options from your left</h4>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="submit()">OK</button>
<button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>