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
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;
}
});
})();
Related
I'm using angularJs. I would like to include a variable into a popover template.
angular.forEach($scope.myList, function (obj) {
obj.details = '<div uib-popover-html="' + template/template.html + '" type="button"' +
'popover-placement="bottom" name="' + obj.id+'" id="' + obj.id+'" ' +
'popover-trigger="\'click\'"> ' +
'<i class="glyphicon glyphicon-eur"></i></div>';
The template is quite simple:
<div>
<button ng-click="onClick(object.id)"></button>
</div>
What is my problem is I don't know how to link this object.id with the forEach. So, for me it is just a template with a param.
Is it possible to do?
I'm using Angular Treeview to build a hierarchy in my website. I've added a Bootstrap Dropdown to each node in the hierarchy, it is displayed when the user clicks on the node label.
The menu items displayed in the dropdown is different depending on the type of the node. All this I've gotten to work.
Now when the user wants to add a node a bootstrap modal is supposed to open for user input. This is where I'm stuck, the modal does not get called at all. I've gotten functions within the directive working using $(".dropdown > ul.dropdown-menu").html($compile(appendThis)(scope));, but if I want to open a model defined in the directive template it does not work.
I've tried the solutions here and here, but they are not working.
Here is a simplified version of the directive template:
template =
'<div class="modal hide fade" id="addThisNode"">'+
'<div class="modal-body">'+
'<p>This Node Body</p>'+
'</div>'+
'</div>'+
'<div class="modal hide fade" id="addOtherNode"">'+
'<div class="modal-body">'+
'<p>Other Node Body</p>'+
'</div>'+
'</div>'+
'<ul>' +
'<li data-ng-repeat="node in ' + treeModel + '">' +
'<i class="normal" '+
'data-ng-hide="node.' + nodeChildren + '.length">'+
'</i> ' +
// Call this funcion when
// the node label is clicked
'<span class="treenode {{node.' + nodeType + '}}" '+
'id="{{node.' + nodeId + '}}"'+
'data-ng-class="node.selected" '+
'data-ng-click="' + treeId +
'.selectNodeLabel(node)">'+
'{{node.' + nodeLabel + '}}'+
'</span>' +
// bootstap dropdown menu
'<div class="dropdown" data-ng-show="node.selected">'+
'<a data-toggle="dropdown"><span class="caret"></span></a>'+
'<ul class="dropdown-menu">'+
// list items get appended here
'</ul>'+
'</div>'+
'</li>' +
'</ul>';
If the user clicks the node label this function is called:
scope[treeId].selectNodeLabel = scope[treeId].selectNodeLabel || function( selectedNode ) {
// set currentNode
scope[treeId].currentNode = selectedNode;
// Get the node type
var nodetype = scope[treeId].currentNode.NodeType;
var appendThis = '';
if (nodetype == 'This'){
appendThis = '<li><a data-target="#addThisNode" href="" data-toggle="modal">Add This Node</a></li>';
}
else if (nodetype == 'Other'){
appendThis = '<li><a data-target="#addOtherNode" href="" data-toggle="modal">Add Other Node</a></li>';
}
$(".dropdown > ul.dropdown-menu").html($compile(appendThis)(scope));
};
Sorry if this is confusing, it's confusing to me as well. But if you have tips on calling modals from inside a directive anything will be appreciated.
After searching through a lot of posts I was able to find an answer. I'm posting it here in case it can help anyone else.
I changed the structure of the template a bit, but the functionality stays the same. The actual problem was in compiling the template. It worked when I compiled it like this:
$(".dropdown > ul.dropdown-menu").html(appendThis);
$compile($(".dropdown > ul.dropdown-menu").contents())(scope);
I have and AngularJS 1.0.7 web application and I´m using AngularStrap select directive. Everything was working fine until I have moved my application to html5Mode. I did that to prettify my URLs.
But now, when I select an option in any bs-select component I´m redirected to index.
HTML
<select class="show-tick" ng-model="selectType" ng-options="boatType as (boatType.name | translate) for boatType in boatTypes" bs-select>
<option value="" selected>{{'BOAT_TYPE' | translate}}</option>
</select>
JS Controller:
$scope.$watch('selectType', function() {
if($scope.selectType != undefined) {
BoatModel.query({type_id: $scope.selectType.id},
function success(result){
if(result.length == 0){
$scope.boatModels = new Array ();
$scope.selectModel = undefined;
SearcherService.setModel(undefined);
}
else{
$scope.boatModels = result.slice();
}
}
);
SearcherService.setType($scope.selectType.id);
$scope.selectModel = undefined;
}
else {
SearcherService.setType(undefined);
$scope.selectModel = undefined;
}
});
I fixed it.
I found out the code generated by the Bootstrap Select directive is not compatible with html5Mode because it inserted an href="#" in each option in the select. When you click on it you are redirected to the index.
Original Code in the directive
createA:function(test, classes) {
return '<a tabindex="-1" href="#" class="'+classes+'">' +
'<span class="pull-left">' + test + '</span>' +
'<i class="icon-ok check-mark"></i>' +
'</a>';
I just fixed it by removing the href, like this:
createA:function(test, classes) {
return '<a tabindex="-1" class="'+classes+'">' +
'<span class="pull-left">' + test + '</span>' +
'<i class="icon-ok check-mark"></i>' +
'</a>';
I'm very new to AngularJs and trying to "wrap" the Smart-Table plugin inside a directive.
I can get the rows but the pagination is not showing
Currently this is what I did.
app.directive('grid', [ function () {
return {
restrict: 'E',
replace: true,
template: function (element, attrs) {
return '<table class="table table-striped">'
+ '<thead>'
+ ' <tr>'
+ '<th st-ratio="20" st-sort="Team">Team</th>'
+ '<th st-ratio="20" st-sort="TeamFreq">Team Freq</th>'
+ '<th st-ratio="10" st-sort="TeamSac">Team Sac</th>'
+ '<th st-ratio="30" st-sort="Priority">Priority</th>'
+ '</tr>'
+ '</thead>'
+ '<tbody>'
+ '<tr ng-repeat="row in dataset">'
+ ' <td>{{row.firstName}}</td>'
+ '<td>{{row.lastName | uppercase}}</td>'
+ '<td>{{row.age}}</td>'
+ '<td>{{row.email}}</td>'
+ '</tr>'
+ '</tbody>'
+ '<tfoot>'
+ '<tr>'
+ '<td colspan="4" class="text-center">'
+ '<div class="pagination pagination-xs m-top-none pull-right" st-pagination="1" st-items-by-page="itemsByPage" st-displayed-pages="4"></div>'
+ '</td>'
+ '</tr>'
+ '</tfoot>'
+ '</table>';
},
scope: {
},
controller: function ($scope) {
},
link: function (scope, elem, attrs) {
var nameList = ['Pierre', 'Pol', 'Jacques', 'Robert', 'Elisa'], familyName = ['Dupont', 'Germain', 'Delcourt', 'bjip', 'Menez'];
function createRandomItem() {
var
firstName = nameList[Math.floor(Math.random() * 4)],
lastName = familyName[Math.floor(Math.random() * 4)],
age = Math.floor(Math.random() * 100),
email = firstName + lastName + '##whatever.com',
balance = Math.random() * 3000;
return {
firstName: firstName,
lastName: lastName,
age: age,
email: email,
balance: balance
};
}
scope.rowCollection = [];
for (var j = 0; j < 10; j++) {
scope.rowCollection.push(createRandomItem());
}
scope.dataset = [].concat(scope.rowCollection);
}
};
}]);
My html contains this tag
<grid st-table="dataset"></grid>
This code is just a test, the data will be passed using a service.. and the template will be dynamic.
I need some help :-)
Thanks
I couldn't get the pagination to show when I was using smart-table in a directive the other day. Turns out it had nothing to do with the directive and I just pulled/updated to the very latest version of smart-table from GitHub and it all worked. I started looking at the what had changed but got side tracked and moved onto something more productive, happy that it was now working.
Version I have that appears to be working fine is tagged 1.4.2.
With dynamic data from a service, however, I think you're going to need to look at the st-safe-src attribute too. Although I'm new to this whole Angular / smart-table business too.
I believe you have to match the st-table value and the st-pagination value to get the pagination to show.
took this from the AngularJS tutorial egghead.io, it never seems to hit the logchore function:
app.controller("ChoreCtrl", function($scope) {
$scope.logChore = function() {
alert("is done");
}
})
jsfiddle: http://jsfiddle.net/dingen2010/sFnAr/1/
Just replace ngclick="done()" to ng-click="done()"
template: '<input type="text" ng-model="chore">' + ' {{chore}}' + ' <div class="button" ng-click="done()">i m done</div>'
taken from the fiddle