ng-repeat a block of html - angularjs

I have two input forms, Date To and Date From that I want to repeat using ng-repeat. Right now, it's a block oh HTML text and I'm not sure what's the most efficient way of doing this except concatinating all the div elements the js side with the plus symbol.
ex. '<div class="container"> + bla bla + ...'
<div class="modal-body">
<!--Time From-->
<div class="row">
<div class="col-md-3 date-and-time-from" >
<label>Date/Time From</label>
</div>
<div class="col-md-9">
<div class="form-group">
<span class="date-field">
<ms-date-time-picker ng-model="newResource.booking[0].startDateTime" placeholder="From"></ms-date-time-picker>
</span>
</div>
</div>
</div>
<!--Time To-->
<div class="row">
<div class="col-md-3 date-and-time-to" >
<label>Date/Time To</label>
</div>
<div class="col-md-9">
<div class="form-group">
<span class="date-field">
<ms-date-time-picker ng-model="newResource.booking[0].endDateTime" placeholder="To" ></ms-date-time-picker>
</span>
</div>
</div>
</div>
</div>
The goal of my application is that a user chooses how many bookings he will store in his array in the first modal. Then he clicks a button, and the above html text will appear in the second modal. Based on the number of bookings the user chooses (let's say 3), the html above will repeat 3 times in a modal (there will be 3 Date From/Date To forms in a vertical list).
Any suggestions would be appreciated!

It is simple to use ng-repeat just go through the documentation here.
I see that in your question you are appending divs like:
htmlString ='<div id="1">Bla</div>'+'<div id="2">Bla</div>';
ngRepeat directive will be used on the HTML itself. Like this:
arrayOfItems = [1,2,3]; //in your controller
<!-- in your html -->
<div ng-repeat="item in arrayOfItems" id="{{item}}">
Bla
</div>

First Modals HTML
Number of bookings:
<input type="text" ng-model="numberOfBooking" name="numberOfBooking" />
<button type="button" ng-click="confirmNumberOfBooking()">Confirm Booking</button>
First Modal Controller
$scope.numberOfBooking = 1;
$scope.newResource.bookings = [
{
fromDateAndTime: "datetime",
toDateAndTime: "datetime"
}
];
$scope.confirmNumberOfBooking = confirmNumberOfBooking;
function confirmNumberOfBooking(){
for(var i = 0; i < $scope.numberOfBooking; i++){
$scope.newResource.bookings.push({fromDateAndTime: "datetime",toDateAndTime: "datetime"});
}
}
Second Modal HTML
<div ng-repeat="booking in newResource.bookings track by $index">
<!--Time From-->
<div class="row">
<div class="col-md-3 date-and-time-from" >
<label>Date/Time From</label>
</div>
<div class="col-md-9">
<div class="form-group">
<span class="date-field">
<ms-date-time-picker ng model="newResource.booking[$index].startDateTime" placeholder="From"></ms-date-time-picker>
</span>
</div>
</div>
</div>
<!--Time To-->
<div class="row">
<div class="col-md-3 date-and-time-to" >
<label>Date/Time To</label>
</div>
<div class="col-md-9">
<div class="form-group">
<span class="date-field">
<ms-date-time-picker ng-model="newResource.booking[$index].endDateTime" placeholder="To" ></ms-date-time-picker>
</span>
</div>
</div>
</div>

Related

Common ng-model value in ng-repeat

I am listing div using ng-repeat to in following manner and include form-template which is assigned. I have several form-template and want to include in following manner. They can have nested ng-repeat to include same template. So i am using "trackerWidgetData" variable in my template. its working fine but inner ng-repeat have different variable(dependWidgetData).
How to use common model value for form-template. If we are including it in nested ng-repeat.
<div class="listview-trckr-new clearfix" ng-repeat="trackerWidgetData in optionsList.inlineTemplates | filter:options.trackerSearchClient track by $index">
<div class="listview-trckr-new-top">
<div class="listview-trckr-new-left clearfix">
<h3>{{trackerWidgetData.trackerName}}</h3>
</div>
<div class="listview-trckr-new-right">
<div ng-if="trackerWidgetData.template" ng-include src="'/public/views/tracker-templates/'+trackerWidgetData.template"></div>
</div>
</div>
<div class="listview-trckr-new-btm" ng-if="trackerWidgetData.dependendTrackers && trackerWidgetData.dependendTrackers.length">
<div class="listview-trckr-new-top" ng-repeat="dependWidgetData in trackerWidgetData.dependendTrackers">
<div class="listview-trckr-new-left clearfix">
<h3>{{dependWidgetData.trackerName}}</h3>
</div>
<div class="listview-trckr-new-right">
<div ng-if="dependWidgetData.template" ng-include src="'/public/views/tracker-templates/'+dependWidgetData.template"></div>
</div>
</div>
</div>
</div>
I have many different type template like this.
<script type="text/ng-template" id="addManualContent.tpl">
<ng-form name="stepAddForm" novalidate >
<div class="new-weight-tracker">
<div class="weight-auto-fil-input">
<input type="text" name="value" ng-model="form[trackerWidgetData.uniqueFormId].trackerValue.values[0].value" />
<div class="new-trkr-select">
<select class="weight-selectbox" name="foodUnit" ng-model="form[trackerWidgetData.uniqueFormId].trackerValue.values[0].unitId" ng-options="opt.id as opt.unitName for opt in trackerWidgetData.unitType.units">
</select>
</div>
</div>
<div class="new-tracker-cale">
<adm-dtp ng-model='form[trackerWidgetData.uniqueFormId].trackerValue.dateTime' maxdate="{{optionsList.maxTimeStamp}}" options='{format: toUpperString(dateFormat) + "hh:mm", default: todayTimestamp, freezeInput:false}'>
<input name="date" ng-model="form[trackerWidgetData.uniqueFormId].trackerValue.dateTime" ng-click="initDTP(form[trackerWidgetData.uniqueFormId].trackerValue.dateTime)" type="text" ng-required="true" dtp-input>
<i aria-hidden="true" class="fa fa-calendar" ng-click="initDTP(form[trackerWidgetData.uniqueFormId].trackerValue.dateTime)" dtp-toggle></i>
</adm-dtp>
</div>
<div class="new-logit-btn">
<a ng-href="" ng-class="{'disable-anchor-btn': optionsList.inAddProcess}" ng-click="optionsList.inAddProcess ? null : addTrackerValue(stepAddForm, trackerWidgetData)" class="fill_button">Log It</a>
</div>
</div>
</ng-form>
</script>
The Question is How to pass model value to ng-template when including it?

Taking user in put in a Controller Array

I am new with Angularjs. I want to take user input in my array of the controller
This is the html
<div class="col-md-6">
<div class="row text-center">
<div class="col-xs-6">
<h4> data A:</h4>
</div>
<div class="col-xs-6">
<h4><input type="text" class="form-control" ng-model="a"></h4>
</div>
</div>
<div class="row text-center">
<div class="col-xs-6">
<h4>data B:</h4>
</div>
<div class="col-xs-6">
<h4><input type="text" class="form-control" ng-model="b"></h4>
</div>
</div>
<div class="row btn-primary text-center" ng-click="showData()">
<i class="glyphicon glyphicon-folder-open" aria-hidden="true">
<h4>data</h4>
</i>
</div>
and the controller
$scope.infoSet =[
{
"key" : "data A",
"value" : $scope.a
},
{
"key" : "data B",
"value" : $scope.b
}];
$scope.showData = function () {
console.log($scope.infoSet);
console.log($scope.a);
console.log($scope.b);
}
How can i do that ? i have tried Difficulty with ng-model, ng-repeat, and inputs .
But i cant put the input in proper format with this. please help . thanks in advance
Remove $scope.a and $scope.b, and use
ng-model="infoSet[0].value"
and
ng-model="infoSet[1].value"
Or use ng-repeat
<div class="row text-center" ng-repeat="info in infoSet">
<div class="col-xs-6">
<h4>{{info.key}}</h4>
</div>
<div class="col-xs-6">
<h4><input type="text" class="form-control" ng-model="info.value"></h4>
</div>
</div>

Bootstrap column pushing/pulling issue

I thought that I understand how pushing and pulling columns work...but I guess I was wrong. I have the following jsfiddle where I created a very simple grid. It looks like this while in xs:
Insured Name
Face AMount
Gender
Product
and then when I am in sm, it looks like thus:
Insured Name | Face Amount
Gender | Product
I'm all good thus far. However, when I get to md I need the Face Amount and Gender fields to switch places. As you can see (from the fiddle) I tried to push the FaceAmount field 6 columns (I expected that this field would then be pushed down into the next row) and then pull the Gender field 6 columns (thus pulling it up a row).
However, if you pull the fiddle out to the md viewpoint...the FaceAmount column is pushed way off to the right (instead of wrapping down) and the Gender field is pulled way to the left to the point where you can't even see it anymore.
Is there something that I am missing? I would have expected that since I am pushing the FaceAmount field passed the 12 column mark that it would wrap down. Similarly, I expected the Gender field to be pulled up to the previous row.
I think you are right by saying:
I am pushing the FaceAmount field passed the 12 column mark
Also, you seem to understand to use push and pull. The problem in the jsfiddle is that you trying to rearrange between two different rows. For example, you can push Insured Name and pull Face Amount using:
<div class="container">
<div class="row">
<div class="col-sm-6 col-md-4">
<div class="well"> 1 </div>
</div>
<div class="col-sm-6 col-sm-push-0 col-md-4 col-md-push-4">
<div class="well"> 3 </div>
</div>
<div class="col-sm-6 col-sm-pull-0 col-md-4 col-md-pull-4">
<div class="well"> 2 </div>
</div>
<div class="clearfix hidden-sm"></div>
<div class="col-sm-6 col-md-4">
<div class="well"> 4 </div>
</div>
<div class="col-sm-6 col-md-4">
<div class="well"> 5 </div>
</div>
<div class="col-sm-6 col-md-4">
<div class="well"> 6 </div>
</div>
</div>
</div>
Result:
But while doing between different rows it doesn't work that way. From this article:
As pushing and pulling works by setting a left or right property it will never wrap to another row as you noted.
Here is a very good article to understand how push and pull works.
This work! http://jsfiddle.net/ujrwyrbr/68/
<div class="row">
<div class="col-sm-6 col-md-12">
<div class="row">
<div class="col-md-6">
<div class="row">
<div class="row">
<div class="col-xs-6">
Insured Name:
</div>
<div class="col-xs-6">
Some Name
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="row">
<div class="col-xs-6">
Face Amount:
</div>
<div class="col-xs-6">
Some Amount
</div>
</div>
</div>
</div>
</div>
<div class="col-md-12 col-sm-6">
<div class="row">
<div class="col-md-6">
<div class="row">
<div class="col-xs-6">
Gender:
</div>
<div class="col-xs-6">
Male
</div>
</div>
</div>
<div class="col-md-6">
<div class="row">
<div class="col-xs-6">
Product:
</div>
<div class="col-xs-6">
Some Product
</div>
</div>
</div>
</div>
</div>
</div>
You can get this to work if you can use the col-md-3 viewport on md and show 4 columns across.
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-3">
<div class="row">
<div class="col-xs-6 trRight">
Insured Name:
</div>
<div class="col-xs-6 trLeft">
x
</div>
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-3 col-md-push-3">
<div class="row">
<div class="col-xs-6 trRight">
Face Amount:
</div>
<div class="col-xs-6 trLeft">
x
</div>
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-3 col-md-pull-3">
<div class="row">
<div class="col-xs-6 trRight">
Gender:
</div>
<div class="col-xs-6 trLeft">
x
</div>
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-3">
<div class="row">
<div class="col-xs-6 trRight">
Product:
</div>
<div class="col-xs-6 trLeft">
x
</div>
</div>
</div>
</div>
</div>
Remember too that nested cols should be inside another row.
http://www.codeply.com/go/AMAmnvznK6

How to add plus minus symbol to a bootstrap accordion along with angularjs

I am using bootstrap accordion in angular js. The panels are opening If I click on the heading directly but I need to add plus/minus symbols for each heading. Please help me out in achieving the functionality like If I click plus icon, panel should be open and If I click on minus symbol, the panel should be closed. Thanks in advance.
Sorry, I Cannot able to add fiddle or plunker as the data is coming from my local database.
<uib-accordion close-others="oneAtATime" class="accordion-data">
<uib-accordion-group heading="title from database" ng-repeat="values are coming from database">
<div class="row">
<div class="col-md-12">
<div class="row row-head">
<div class="col-md-6 col-xs-6">
</div>
</div>
<div class="row item-head accordion-content" ng-repeat="values are coming from database">
<div class="col-md-4 col-xs-4"><input type="checkbox" ng-model="check" >data from database
</div>
<div class="col-md-8 col-xs-8 service-data">
<div class="row">
<div class="col-md-2 col-xs-2"></div>
<div class="col-md-2 col-xs-2">
<input type="number" class="form-data" ng-disabled="!check">
</div>
<div class="col-md-2 col-xs-2">
<input type="number" name="times" class="form-data" ng-disabled="!check">
</div>
<div class="col-md-2 col-xs-2">
<input type="text" class="form-data" ng-disabled="!check" ng-readonly="true" >
</div>
<div class="col-md-2 col-xs-2">
<input type="text" class="form-data" ng-disabled="!check" ng-readonly="true" >
</div>
<div class="col-md-2 col-xs-2">
<input type="text" class="form-data" ng-disabled="!check" ng-readonly="true" >
</div>
</div>
</div>
</div>
</div>
</div>
</uib-accordion-group>
</uib-accordion>
I don't know the specifics about your implementation, but you could make use of the CSS :before pseudo-element like I have done in this simple fiddle.
If you have a way of selecting the accordion header by the open/closed state, then you can do something similar to this:
.accordion li.open:before {
content: '- ';
}
.accordion li.closed:before {
content: '+ ';
}
glyphicon glyphicon-plusIf you want to handle this in javascript, you could use the collapse event described here.
shown.bs.collapse will fire when a collapse element has been made visible to the user (will wait for CSS transitions to complete).
hidden.bs.collapse will fire when a collapse element has been hidden from the user (will wait for CSS transitions to complete).
$('#yourCollapseDiv').on('shown.bs.collapse', function () {
$(".glyphicon").removeClass("glyphicon glyphicon-minus").addClass("glyphicon glyphicon-plus");
});
$('#yourCollapseDiv').on('hidden.bs.collapse', function () {
$(".glyphicon").removeClass("glyphicon glyphicon-plus").addClass("glyphicon glyphicon-minus");
});
It uses glyphicons to display the + and -

How to get the number of comments on a post

I'm trying to get the number of comments a post has, but can't find a way to do it. I have a plunk if you want to check it.
<div class="anuncios">
<div class="panel panel-default" ng-repeat="posts in post | orderBy:'-FechaAnuncio'">
<div class="panel-heading" style="overflow:hidden;">
<p>{{posts.NombreLocal}}</p>
</div>
<div class="panel-body">
<p>{{posts.Anuncio}}</p>
<hr>
<small>{{posts.FechaAnuncio | date:'HH:mm:ss dd-MM-yyyy'}}</small>
<button class="btn btn-primary" style="float:right;" ng-click="showme = !showme"><span ng-hide="showme">Show Comments: {{posts.comments.length}}</span><span ng-show="showme">Hide Comments</span></button>
<div>
<div class="well" ng-show="showme">
<div class="input-group">
<input type="text" class="form-control" placeholder="Comment Here..." ng-model="comment.text">
<span class="input-group-btn">
<button class="btn btn-default" type="button" ng-click="addComment(posts, comment)">Publish!</button>
</span>
</div>
<!-- /input-group -->
<div class="cmt">
<div class="panel" ng-repeat="comments in posts.comments">
<p>{{comments.NombreLocal}}</p>
<p>{{comments.FechaComentario | date:'HH:mm:ss dd-MM-yyyy'}}</p>
<hr>
<p>{{comments.Comment}}</p>
<p></p>
</div>
<!--FIN DE COMENTARIOS -->
</div>
</div>
</div>
</div>
</div>
</div>
http://plnkr.co/edit/AIx0x7?p=preview
The number of comments should display next to "Show Comments:"
This is my Firebase Structure:
Firebase structure image
Since comments is an object, the easiest thing to do is to count the number of keys it has. You'll need to add this to your controller, or you could create your own filter.
app.controller('AnunciosCtrl', function($scope, $firebaseArray, $timeout) {
...
$scope.sizeOf = function(obj) {
return Object.keys(obj || {}).length;
};
});
HTML:
<span ng-hide="showme">Show Comments: {{ sizeOf(posts.comments) }}</span>
Plunker
In your controller do:
$scope.numComments = $scope.post.comments.length;
In your html :
...>Show Comments: {{numComments}}<...

Resources