Taking user in put in a Controller Array - angularjs

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>

Related

ng-repeat a block of html

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>

Angularjs upload image

I'm trying to create an upload function using angularjs. I have watch and read few tutorials in the internet. Unfortunately all the tutorials are too complicated for me because I'm still new in this field.
<form ng-submit="upload(currentUser())">
<div class="row">
<div class="form-group">
<div class="col-xs-2">
<label>Car</label>
</div>
<div class="col-xs-5">
<input type="text" placeholder="Perodua Myvi" class="form-control" ng-model="newCar" required>
</div>
<div class="col-xs-5"></div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-xs-2">
<label>Price(RM) per day</label>
</div>
<div class="col-xs-5">
<input type="text" placeholder="80" class="form-control" ng-model="newPrice" required>
</div>
<div class="col-xs-5"></div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-xs-2">
<label>Business Area</label>
</div>
<div class="col-xs-5">
<input type="text" placeholder="Universiti Malaysia Sabah" class="form-control" ng-model="businessArea" required>
</div>
<div class="col-xs-5"></div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-xs-2">
<label>Insert Car Image</label>
<br>
</div>
<div class="col-xs-5">
<!--<button type="button" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-picture"></span> Image
</button>-->
<input type="file"/>
</div>
<div class="col-xs-5"></div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-xs-2">
</div>
<div class="col-xs-5">
<button type="submit" class="btn btn-primary btn-sm pull-right">
<span class="glyphicon glyphicon-globe"></span> Publish
</button>
</div>
<div class="col-xs-5"></div>
</div>
</div>
<br>
<br>
</form>
app.controller('postadCtrl', [
'$scope',
'auth',
function($scope, auth) {
$scope.currentUser = auth.currentUser;
$scope.posts = [];
$scope.upload = function(user) {
$scope.posts.push({
company_name: user,
car_type: $scope.newCar,
seaters: 5,
price: $scope.newPrice,
contact: 038880000,
location: $scope.businessArea
});
};
}
]);
So how do I do this in the simplest form and explanation? And how does my router and module will look like?
The output should look like this.

how to validate two select boxes in angular js

this is my html form that have to use to get the details but when i use the required attribute in the select tag, only the first one is validated and not the second one or an other field after the select tag, for eg if i select option in the first select and click post project then there is no error about the validation, and if i dont select the data from the first select tag there is validation error popup saying that the field is required,
<form name="myForm" role="form" action="#">
<div class="well" style="margin-left:20px; margin-right:20px">
<div class="row" style="margin-left:5px; margin-right:5px">
<div class="col-lg-12">
<h4 style="color:darkblue">What type of project do you require?</h4>
</div>
</div>
<br>
<div class="row" style="margin-left:5px; margin-right:5px">
<div class="col-md-6 col-lg-6">
<select name="drop1" class="form-control" ng-model="project.match1">
<option value="">-- Select Category --</option>
<option ng-repeat="items in serviceCategory" value="{{items.ServiceCategoryId}}">{{items.ServiceCategoryName}}</option>
</select>
</div>
<div class="col-md-6 col-lg-6">
<select class="form-control" ng-model="project.match2" name="drop2">
<option value="">-- Select Sub Category --</option>
<option ng-repeat="a in service" ng-if="a.ServiceCategoryId==project.match1">
{{a.ServiceName}}
</option>
</select>
</div>
</div>
<br>
<div class="row" style="margin-left:5px; margin-right:5px">
<div class="col-md-12 col-lg-12">
<textarea ng-model="project.ServiceCategories" placeholder="Add Service Categories here" rows="3" style="resize:none; width:100%"></textarea>
</div>
</div>
</div>
<div class="well" style="margin-left:20px; margin-right:20px">
<div class="row" style="margin-left:5px; margin-right:5px">
<div class="col-md-12 col-lg-12 text-left">
<h4 style="color:darkblue">What is your Project about</h4>
</div>
</div>
<br>
<div class="row" style="margin-left:5px; margin-right:5px">
<div class="col-md-12 col-lg-12 text-left">
Describe your Project<br>
</div>
</div>
<div class="row" style="padding-top:10px; margin-left:5px; margin-right:5px">
<div class="col-md-12 col-lg-12">
<input type="text" ng-model="project.DescribeProject" style="width:100%">
</div>
</div>
<br>
<div class="row" style="margin-left:5px; margin-right:5px">
<div class="col-md-6 col-lg-6">
What is your Project about?
</div>
<div class="col-md-6 col-lg-6">
</div>
</div>
<div class="row" style="padding-top:10px; margin-left:5px; margin-right:5px">
<div class="col-md-12 col-lg-12">
<textarea ng-model="project.AboutProject" ng-trim="false" rows="3" cols="70" ng-maxlength="5000" style="resize:none; width:100%;"></textarea>
</div>
</div>
<br>
<div class="row" style="margin-left:5px; margin-right:5px">
<div class="col-md-12 col-lg-12">
Attach files
</div>
</div>
<div class="row" style="padding-top:10px; margin-left:5px; margin-right:5px">
<div class="col-md-12 col-lg-12">
<!--attach file code-->
<div style="height:30px; width:100%;">
</div>
</div>
</div>
<div class="row" style="margin-bottom:15px; margin-left:5px; margin-right:5px">
<div class="col-md-1 col-lg-1"></div>
<div class="col-md-10 col-lg-10">
<div class="text-right">
<button class="btn btn-default btn-primary" type="submit" ng-click="showData(project)">Post Project</button>
<button class="btn btn-default" data-dismiss="modal" aria-label="Close" style="color:black; border-color: #2e6da4">Close</button>
</div>
</div>
<div class="col-md-1 col-lg-1"></div>
</div>
</div>
</form>
The Script is
var app = angular.module("MyApp", []);
app.controller('MainCtrl', function ($scope, $http) {
$http.get('/JsonData/ServiceCategory.json').success(function (response) {
console.log("Service Category Connected");
$scope.serviceCategory = response;
});
$http.get('/JsonData/Service.json').success(function (res) {
console.log("Service Connected");
$scope.service = res;
});
$scope.showData = function (inputData) {
var Project = new Object();
Project.Category = inputData.match1;
Project.SubCategory = inputData.match2;
Project.ServiceCategories = inputData.ServiceCategories;
Project.Description = inputData.DescribeProject;
Project.AboutProject = inputData.AboutProject;
var ProjectJson = JSON.stringify(Project);
alert(ProjectJson);
}
});

d.setclass is not a function in ngMessages module

In one of my angularjs pages, I am using ngMessages module for form validation. While loading that page I am getting folowing error in console.
"angular.js:9400TypeError: d.setClass is not a function
at render (angular-messages.js:413)"
No idea why. Any help would be appreciated.
here is the HTML
<div class="extranet-content">
<div class="container-list">
<form name="personalForm" ng-submit="personalForm.$valid && saveUsers()" novalidate>
<div class="list-header" ng-show="all_users.length > 0">
<div class="row">
<div class="col-xs-1"></div>
<div class="col-xs-1">
<h3>
Id
</h3>
</div>
<div class="col-xs-3">
<h3>
Name
</h3>
</div>
<div class="col-xs-3">
Login
</div>
<div class="col-xs-2">
Sign
</div>
<div class="col-xs-2">
password
</div>
<div class="col-xs-12">
<div class="border"></div>
</div>
</div>
</div>
<div class="list-content" ng-class="{ 'noHeader' : !all_users.length }">
<div class="row" ng-show="saveErrors.length > 0" ng-repeat="error in saveErrors track by $index">
<div class="col-xs-12">
</div>
<div class="col-xs-12"></div>
</div>
<div class="row clearfix"
ng-repeat-start="user in all_users = (users | filter: searchUser) | orderBy : predicate:reverse track by $index"
ng-class="{ 'marginTop' : $first}">
<div class="col-xs-1">
</div>
<div class="col-xs-1"></div>
<div class="col-xs-3">
</div>
<div class="col-xs-3">
<input type="text" name="txtLogin" id="txtLogin_{{$index}}" ng-model="user.Login">
</div>
<div class="col-xs-2">
<ng-form name="signForm">
<input maxlength="7" type="text" name="txtSign" id="txtSign_{{$index}}" ng-model="user.Sign"
required>
<span ng-messages="listFormSubmitted && signForm.txtSign.$error" role="alert" class="error">
<span ng-message="required">required</span>
</span>
</ng-form>
</div>
<div class="col-xs-2"></div>
</div>
<div class="row group" ng-repeat-end>
<div class="col-xs-12" ng-if="!$last">
<div class="borderBottom"></div>
</div>
</div>
</div>
</form>
</div>
</div>

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