Internet Explorer modal directive in angular js - angularjs

Hello I had written following directive in angular js to use modal:
appname.directive("modal", function () {
return {
template: '<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">' +
'<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: 'EA',
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;
});
});
}
};
});
Its work fine in Chrome, IE 9 and firefox now I want it to work it in IE 8 , some blog says used document.createElement('directivenmae'), but its just create simple html element not include content of this template.
It would be nice if anyone provide suggestion for my this issue.

Ok, so first of all you will have to read the entire section on IE8 integration on Angular's website. There is a number of things that you have to do in order to make it work. Start there.
"They" are right that you have to call this piece of code:
document.createElement(yourTagName)
It's not intended to render anything, it is aimed to let IE8 know that you will be having some custom HTML tags in your markup.
Finally, please note that IE8 is not supported in AngularJS 1.3.+, so be advised and use latest 1.2.x branch.
After all that you will be good to go - I'm also using Bootstrap + Angular in IE8 and it works fine :)

Related

How to reload the page in ui-router

i have a delete button, for the confirmation from the user, i am using directive for modal.
this is the directive code
app.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" ng-click="close()">×</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;
});
});
}
};
});
this is the controller code
$scope.deletePlace = function(place) {
if (place._id) {
var url = '/api/places/' + place._id;
$http.delete(url, {})
.then(function(response) {
$scope.showModal = false;
$state.transitionTo('dashboard.places.list', null, { reload: true, inherit: true, notify: true });
}, function(response) { // fail
$scope.errorMessage = true;
});
}
}
after clicking ok button on delete modal, the modal will hide, but the black screen remains same and the buttons on page or not clickable, untill i refresh the page manually. Is there any way to remove the black screen after clicking ok button on confirmation modal. If i click the refresh manually, it will work. i don't want to refresh it manually. i want it to be refreshed automatically or from other way to hide the black screen.
Try $state.reload() It should work in most cases if you're using latest version of ui-router.
If that doesn't work try routing to the same page using $state.go() or $state.transitionTo() with additional parameter as {reload: true}.
If that also doesn't work create a auto executing method to initialize your controller and call that method again.
activate();
function activate(){
}

how to create directive for daterangepicker?

I came across this nice datepicker on this site: http://luisfarzati.github.io/ng-bs-daterangepicker/. It works fine here : http://plnkr.co/edit/qmj5urjBb4TdtUYCuwap?p=preview
However I would like to make a directive to reuse in my site:
app.directive('dateRange', function () {
return {
restrict: 'E',
templateUrl: 'picker.html'
};
});
picker.html:
<link rel="stylesheet" href="http://luisfarzati.github.io/ng-bs-daterangepicker/bower_components/bootstrap-daterangepicker/daterangepicker-bs3.css"/>
<script src="http://luisfarzati.github.io/ng-bs-daterangepicker/bower_components/momentjs/moment.js"></script>
<script src="http://luisfarzati.github.io/ng-bs-daterangepicker/bower_components/bootstrap-daterangepicker/daterangepicker.js"></script>
<script src="http://luisfarzati.github.io/ng-bs-daterangepicker/ng-bs-daterangepicker.js"></script>
<input type="daterange" ng-model="dates4" ranges="ranges">
I used my directive in an index.html:
<date-range></date-range>
Even though it works in the 1st plunkr I cant get it too work as a directive. Why am I getting this error:
Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.3.0-beta.14/$injector/modulerr?p0=filtersampl….com%2Fajax%2Flibs%2Fangularjs%2F1.3.0-beta.14%2Fangular.min.js%3A18%3A139)
plunkr:http://plnkr.co/edit/nr8iOIpIDSf26DxiWEJF?p=preview
I have another code for datepicker try this if you found helpful,
datepicker.directive('datepicker', function($compile,$modal) {
return {
restrict: "A",
link: function(scope, element, attr) {
element.addClass("datepicker-input");
element.after("<span class='datepicker-icon'><i class='fa fa-calendar'></i></span>");
if (attr.withmore) {
element.addClass("withMore");
element.next(".datepicker-icon").after('<i class="fa fa-search-plus"></i>');
$compile(element.next(".datepicker-icon").next('.more-datepicker-icon'))(scope);
}
element.datepicker({
format: attr.format ? attr.format : 'dd-mm-yyyy',
autoclose: true,
todayHighlight: true
});
scope.selectdaterange = function(){
scope.modalInstance = $modal.open({
template : '<div class="modal-header">'+
'<h3 class="modal-title">Select Daterange</h3>'+
'</div>'+
'<div class="modal-body">'+
'You have not selected any News to delete. Please select at least one News to delete.'+
'</div>'+
'<div class="modal-footer">'+
'<button class="btn btn-important" ng-click="deleteCancel()">OK</button>'+
'</div>',
scope : scope
});
}
}
};
});

How to call directive template on click?

I am trying to make a reusable modal dialog and I would like to load directive template on click directive itself..
function modalDialog() {
var directive = {
restrict: 'A',
link: linkFunc,
template: '<div class="modalBox--blur">' +
'<div class="modalBox">' +
'<h3>' {{title}} '</h3>' +
'<h4>' {{text}} '</h4>' +
'<button ng-click="answer(true)">Cancel</button>' +
'<button ng-click="answer(false)">Ok</button>' +
'</div>' +
'</div>',
scope: {
title: '=dialogTitle',
text: '=dialogTxt'
},
transclude: true
};
return directive;
function linkFunc($scope, element, attrs) {
element.on('click', function () {
$scope.newEl = element.parent();
$scope.newEl.append(...template Here...???);
});
}
}
This is how directive is set in the view:
<button
modal-dialog
dialog-title="modalBox.title"
dialog-txt="modalBox.subText"
type="button"
ng-click="deleteSth()"
class="button">
</button>
I can't figure out how to load template on element click :
element.on('click', function () {
$scope.newEl = element.parent();
$scope.newEl.append(template????);
});
Any tips?
Thank you in advance!
You can get the template with $templateCache
Like $templateCache.get('templateId.html')
Solution was compiling the template:
scope.modal = $compile(' <div class="modalBox--blur">' +
'<div class="modalBox">' +
'<h3>{{title}}</h3>' +
'<h4>{{text}}</h4>' +
'<button ng-click="dialogAnswer(true)">Annuleren</button>' +
'<button ng-click="dialogAnswer(false)">Ok</button>' +
'</div>' +
'</div>')(scope);
element.on('click', function () {
scope.newEl = element.parent();
scope.newEl.append(scope.modal);

ng-model two way binding not working with ui bootstrap modal

I have following code for working with ui bootstrap modal. I am having a input field whose value has to be captured on the controller. But the input field value is not getting reflected on the controller after an value is entered on the modal.
angular.module('myApp')
.controller('mainController', ['$scope', '$modal', function($scope, $modal) {
$scope.openModal = function() {
$modal.open({
templateUrl: 'modal.html',
controller: BoardController
});
};
}])
.directive('modalDialog', function() {
return {
restrict: 'E',
replace: true,
transclude: true,
template:
'<div class="modal-content">' +
'<div class="modal-header">' +
'<h4 ng-bind="dialogTitle"></h4>' +
'</div>' +
'<div class="modal-body" ng-transclude></div>' +
'<div class="modal-footer">' +
'<button type="button" class="btn btn-default" ' +
'ng-click="cancel()">Close</button>' +
'<button type="button" class="btn btn-primary" ' +
'ng-click="ok()">Save</button>' +
'</div>' +
'</div>'
};
});
var BoardController = function ($scope, $modalInstance) {
$scope.dialogTitle = 'Create new item';
$scope.placeholder = 'Enter item name';
$scope.inputname = '';
$scope.ok = function () {
console.log($scope.inputname);
$modalInstance.dismiss('cancel');
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
};
In 'modal.html' i have the following code:
<modal-dialog>
<input type="text" class="form-control" id="enter-name"
ng-model="inputname" placeholder={{placeholder}}>
{{ inputname }}
</modal-dialog>
So, after entering some text into the inputfield when i click the save the following line under $scope.ok() prints blank.
console.log($scope.inputname);
I guess this has something to do with scopes or may be transclusion. But i am not able to figure out whats causing this. I couldnt find the updated value in developer console also.
The problem here is transclusion. ngTransclude directive creates one more scope, but it is a sibling scope. Using transclusion makes it very difficult to access your scope. In your case you could retrieve model value like this:
$scope.ok = function () {
console.log($scope.$$childHead.$$nextSibling.inputname);
$modalInstance.dismiss('cancel');
};
But of course this is terrible. Fortunately, you can control what scope transclusion will use for rendered template if you make transclusion manually. For this you need to use link function with the fifth argument which is transclude function.
Your directive will become (note, that you don't use ng-tranclude directive in template anymore):
.directive('modalDialog', function() {
return {
restrict: 'E',
replace: true,
transclude: true,
template:
'<div class="modal-content">' +
'<div class="modal-header">' +
'<h4 ng-bind="dialogTitle"></h4>' +
'</div>' +
'<div class="modal-body"></div>' +
'<div class="modal-footer">' +
'<button type="button" class="btn btn-default" ng-click="cancel()">Close</button>' +
'<button type="button" class="btn btn-primary" ng-click="ok()">Save</button>' +
'</div>' +
'</div>',
link: function(scope, element, attrs, controller, transclude) {
transclude(scope, function(clone) {
var modalBody = element[0].querySelector('.modal-body');
angular.element(modalBody).append(clone);
});
}
};
});
Demo: http://plnkr.co/edit/I7baOyjx4pKUJHNkxkDh?p=preview

Angularjs directive attribute can't wait for loading data

I have a directive for showing a 'share to social networks menu'. It works fine in normal page but when i use it on a lightbox (that fetchs it's data after clicking on an item), it fails to pass attributes to share menu directive without any error. The variables are empty.
<item-actions data-itemid="{{popup.id}}"></item-actions>
Directive:
var itemActions_tmpl =
'<div>' +
// share button
'<div class="share-w">' +
'<button type="button" class="button radius share" value="share" >' +
'</button>' +
'<ul>' +
'<li><a target="_blank" data-ng-href="{{::fblink}}">Facebook</a></li>' +
'<li><a target="_blank" data-ng-href="{{::gplink}}">Google +</a></li>' +
'<li><a target="_blank" data-ng-href="{{::twlink}}">Twitter</a></li>' +
'</ul>' +
'</div>' +
'</div>';
jApp.directive('itemActions', ['APP_CONFIG', function(APP_CONFIG){
return {
replace:true,
scope: {},
restrict: 'AE',
template: itemActions_tmpl,
link: function(scope, Elem, Attrs, controller) {
scope.fblink = 'https://www.facebook.com/sharer/sharer.php?u=' + APP_CONFIG.BASE_ITEM_URL + Attrs.itemid;
scope.gplink = 'https://plus.google.com/share?url=' + APP_CONFIG.BASE_ITEM_URL + Attrs.itemid;
scope.twlink = 'http://twitter.com/home?status=' + APP_CONFIG.BASE_ITEM_URL + Attrs.itemid;
}
};
}]);
How can i fix this?
I used $q and problem solved. Just waiting for data and then opening popup.

Resources