Reputation: 604
I've created a new template/datepicker/popup.html template that get's loaded after ui.bootstrap.tpls.
What I'm struggling with is calling a function from ng-click in the new template. I don't want to update the ui.bootstrap.tpls file. I've attempted this two different ways:
1) Less Ideal: Use the select function already within directive datepickerPopup from ui.bootstrap.tpls. I can get this to work partially by converting to .toLocaleDateString() but after the first click I open the calendar again and the calendar still shows current date instead of reflecting the updated date.
"<button class='btn btn-default' ng-click='select(date.setTime(date.getTime()+30 * 86400000).toLocaleDateString())'>30 days</button>" +
2) Ideal: Create a brand new function outside of ui.bootstrap.tpls called addDays(n). I can't reach this function from the new template. I would like to create my own function to call from the new template.
"<button class='btn btn-default' ng-click='addDays(date, 30)'>30 days</button>" +
Plunker:
http://plnkr.co/edit/Klbek5SpOGIA4FXTmlFX?p=preview
Upvotes: 2
Views: 3825
Reputation: 604
I spent more time tinkering with this today and came up with a solution to my own questions.
When you override the template add ng-controller="ControllerName" to the outer element in my case a div. Then you need to add your controller to the module, in my solution I just added the controller right into the popupTemplate.js.
Updated Plunker: http://plnkr.co/edit/YLJW2imcDAbzwsvp58Rl
Javascript FileName popupTemplate.js. Code:
(function() {
'use strict';
angular.module("template/datepicker/popup.html", [])
.run(["$templateCache", function($templateCache) {
$templateCache.put("template/datepicker/popup.html",
"<div ng-controller='ExampleCtrl'>" +
"<ul class=\"dropdown-menu\" ng-style=\"{display: (isOpen && 'block') || 'none', top: position.top+'px', left: position.left+'px'}\" ng-keydown=\"keydown($event)\">\n" +
" <li ng-transclude></li>\n" +
" <li ng-if=\"showButtonBar\" style=\"padding:10px 9px 2px\">\n" +
" <span class=\"btn-group pull-left\">\n" +
" <button type=\"button\" class=\"btn btn-sm btn-info\" ng-click=\"select('today')\">{{ getText('current') }}</button>\n" +
" <button type=\"button\" class=\"btn btn-sm btn-danger\" ng-click=\"select(null)\">{{ getText('clear') }}</button>\n" +
" </span>\n" +
" <button type=\"button\" class=\"btn btn-sm btn-success pull-right\" ng-click=\"close()\">{{ getText('close') }}</button>\n" +
" </li>\n" +
"</ul>\n" +
"<div class=\"dropdown-menu-extend\" ng-style=\"{display: (isOpen && 'block') || 'none', top: position.top+'px'}\">" +
"<h4 >or select a future date here...</h4>" +
"<div class='col-lg-6'>" +
"<h5>Add a new function</h5>" +
"<button class='btn btn-default' ng-click='select(addDays(date, 30))'>30 days</button>" +
"</div>" +
"</div>" +
"</div>" +
"");
}])
.controller('ExampleCtrl', function ($scope) {
$scope.addDays = addDays;
function addDays(date, days) {
var newDate = date.setTime(date.getTime()+days * 86400000); //epoch date
var finalDate = new Date(newDate); //formatdate
console.log(finalDate);
return finalDate;
}
});
})();
Upvotes: 2