show and hide div from a repeat - angularjs

I have a heading and comment to the heading on a repeat.
What I want to do it, keep all the inputs below the posts hidden at first and then, on clicking show input below button, show input for only that post. Basically a toggle but inside a repeat.
$scope.posts = [{"_id":"5","post":"second post","created_at":"2015-01-07T07:11:37.477Z"},{"_id":"542","post":"first post","created_at":"2015-01-07T07:11:30.922Z"}]
<div class="col-sm-12 margin-top-10" data-ng-repeat="post in posts track by $index">
<h4>{{post.post}}</h4>
<button type="button" class="pull-right btn btn-xs">
show input below
</button>
<h6 data-ng-repeat="comment in post.comments track by $index">{{comment}}</h6>
<form name="form.comment">
<input class="col-sm-12" type="text" name="commentText" ng-model="commentText" ng-keyup="$event.keyCode == 13 ? takeComment(post._id, commentText) : null" required>
</form>
<br>
<br>
<br>
</div>

$scope.posts = [{"_id":"5","post":"second
post","created_at":"2015-01-07T07:11:37.477Z"},{"_id":"542","post":"first
post","created_at":"2015-01-07T07:11:30.922Z"}]
<div class="col-sm-12 margin-top-10" data-ng-repeat=
"post in posts track by $index">
<h4>{{post.post}}</h4><button class="pull-right btn btn-xs" type=
"button" ng-click="post.show = !post.show">show input below</button> <--- add ng click
<h6 data-ng-repeat="comment in post.comments track by $index">
{{comment}}</h6>
<form id="form.comment" name="form.comment" ng-show="post.show"> <--- add ng show
<input class="col-sm-12" name="commentText" required="" type=
"text">
</form><br>
<br>
<br>
</div>

Related

Dynamically changing name of ng-show and scope variable

I have retrieving the contents from database using $http.get api and displaying each record using ng-repeat. For each record i have a like and comment button. On clicking on comment button, i will show input box with send button previously it will be hidden(using ng-show). The problem is- on clicking any one of the record comment button,all other records input box with send button will be shown including clicked.
<div ng-repeat="dat in details">
<ul>
<li><b>Product:</b><span> {{dat.product_name}}</span></li>
</ul>
<button style="background-color:#4C97C8;color:white;height:30px" class="btn buttonlike" ng-click="likebtn(dat.id,loginname)"><span class="glyphicon glyphicon-hand-right"></span><strong> Like</strong></button>
<button style="background-color:#4C97C8;color:white;height:30px" class="btn" ng-click="mycomment()"><span class="glyphicon glyphicon-comment"></span><strong> Comment</strong></button>
<div class="input-group" ng-show="comment1">
<input type="text" class="form-control" placeholder="comment" aria-describedby="basic-addon2">
<span class="input-group-addon" id="basic-addon2"><span class="glyphicon glyphicon-send"></span></span>
</div>
</div>
mycomment() method in script looks like-
$scope.comment1= false;
$scope.mycomment = function() {
$scope.comment1= true;
}
How can i change the name of ng-show-"comment1" dynamically(if I change, I have to change the name in script too) ?? is there any other way?
Try this:
<div ng-repeat="dat in details | filter : { product_name : textname} as results">
<hr/>
<p style="color:#4C97C8;" class="lead"><strong>{{dat.summary}}</strong></p>
<ul>
<li><b>Product:</b><span> {{dat.product_name}}</span></li>
<li><b>Product Manager:</b><span> {{dat.first_name}} {{dat.last_name}}</span></li>
<li><b>Description:</b><span> {{dat.description}}</span></li>
</ul>
<button style="background-color:#4C97C8;color:white;height:30px" class="btn buttonlike" ng-click="likebtn(dat.id,loginname)"><span class="glyphicon glyphicon-hand-right"></span><strong> Like</strong></button>
<button style="background-color:#4C97C8;color:white;height:30px" class="btn" ng-click="comment=true"><span class="glyphicon glyphicon-comment"></span><strong> Comment</strong></button>
<div class="input-group" ng-show="comment">
<input type="text" class="form-control" placeholder="comment" aria-describedby="basic-addon2">
<span class="input-group-addon" id="basic-addon2"><span class="glyphicon glyphicon-send"></span></span>
</div>
</div>
Instead of ng-click="mycomment()" and ng-show="comment1": ng-click="comment=true" and ng-show="comment" accordingly. $scope.comment1 and $scope.mycomment are not needed.

New textfield not coming while clicking add new in a loop in angularjs

This is my html block in angular
<div class="form-group" ng-repeat="n in [1, 2, 3, 4, 5, 6]">
<div class="choice-div" ng-if="webfront.customer_fields[$index].input_type == 4">
<label class="col-sm-3 control-label no-padding-right" for="form-field-1">Choices : </label>
<div class="col-sm-9">
<div ng-repeat="textBox in webfront.customer_fields[$index].webfront_field_values" class="addtext-div">
<input ng-model="textBox.value" class="col-xs-10 col-sm-5 addtext_box" type="text" id="{{textBox.id}}" required="" placeholder="Value" autocomplete="off" style="width: 40%;">
<span ng-click="removeTextBox(webfront.customer_fields[$parent.$index].webfront_field_values, $index, textBox.id)" style="margin-left: 20px;cursor: pointer;"> <i title="Cancel this option!" class="raty-cancel cancel-off-png" data-alt="x"></i></span>
</div>
<span class="btn btn-sm btn-success" ng-click="addTextBox(webfront.customer_fields[$index].webfront_field_values)"><i class="ace-icon fa fa-plus"></i> Add Option</span>
<br class="clear"/>
</div>
</div>
This is my output of json formatted angular data
webfront{"id":1,"customer_fields":[{"id":24,"webfront_id":1,"name":"CA1","webfront_field_values":[]},{"id":25,"webfront_id":1,"name":"Education","webfront_field_values":[{"id":14,"webfront_field_id":25,"value":"B.tech"},{"id":15,"webfront_field_id":25,"value":"MCA"},{"id":16,"webfront_field_id":25,"value":"MBA"}],"customer_checkbox":true}]}
Below i have included the image how is my html
While there is data in the input field everthing working fine as i have mentioned above
But while there is no data only add option comes.Also the Add option for new text field not working.
I want there should be a text field on the top of the add option come and while click to the Add option new text field should come with remove option. Can anyone help me to solve this.

Angular validation for only 1 input field inside a form

I'm building a form using Angular 1.1.1 and Ionic.
There are many "wallets" and the user needs to send a new "value" to each of the wallet. My form has a validation for all fields which works fine when the 'submit' button for the form is pressed.
However, I also have a button next to each wallet to send only value to this wallet (not different values to all wallets). When I press it, all the validation errors appear, but I need error to be visible only for the particular wallet.
My form (index.html):
<form name="myForm" ng-submit="sendValues(wallets)" ng-controller="valuesCtrl" novalidate>
<div class="row" ng-repeat="wallet in wallets">
<div class="col item item-input-inset">
<label class="item-input-wrapper item-text-wrap">
<input name="wallet_{{wallet.id}}" type="number" ng-model="wallet.value" type="text" required/>
</label>
<span ng-show="myForm.wallet_{{wallet.id}}.$error.required">!!!</span>
</div>
<div class="col item">{{ wallet.previous }}</div>
<button ng-click="sendValue(wallet)">
<i class="ion-android-send"></i>
</button>
<span class=ng-show="myForm.$submitted==true && myForm.wallet_{{wallet.id}}.$error.required">Required</span>
</div>
<button class="button" type="submit">Submit</button>
</form>
My controller (values.js):
'Use Strict';
angular.module('App')
.controller('valuesCtrl', function($scope, $localStorage, UserService, $state) {
$scope.sendValues = function(wallets){
if ($scope.myForm.$valid) {
...
} else {
$scope.myForm.submitted = true;
}
},
$scope.sendValue = function(wallet){
if (wallet.value == null) {
$scope.myForm.submitted = true;
} else {
...
}
}
})
You need to create a form for each wallet
This is due to your html name attributes has same value inside ng-repeat
Use $index in your name field for differentiate all the name attribute.
<form name="myForm" ng-submit="sendValues(wallets)" ng-controller="valuesCtrl" novalidate>
<div class="row" ng-repeat="wallet in wallets">
<div class="col item item-input-inset">
<label class="item-input-wrapper item-text-wrap">
<input name="wallet_{{$index}}" type="number" ng-model="wallet.value" type="text" required/>
</label>
<span ng-show="myForm.wallet_{{wallet.id}}.$error.required">!!!</span>
</div>
<div class="col item">{{ wallet.previous }}</div>
<button ng-click="sendValue(wallet)">
<i class="ion-android-send"></i>
</button>
<span class=ng-show="myForm.$submitted==true && myForm.wallet_{{$index}}.$error.required">Required</span>
</div>
<button class="button" type="submit">Submit</button>
</form>
You have to create a form inside form again. But as per HTML standard you can not have nested form. But angular provided that ability to have nested form but the inner form should be ng-form. Which mean you are going to wrap form & inside that you can find multiple ng-form's.
So you should have ng-form="innerForm" which will keep track of each repeated form.
Other thing which I observed is, you did mistake while using ng-show(you had {{}} inside ng-show expression, which would not work). To fix it you could access object via its key like ng-show="innerForm['wallet_'+wallet.id].$error.required"
Markup
<form name="myForm" ng-submit="sendValues(wallets)" ng-controller="valuesCtrl" novalidate>
<div ng-form="innerForm" class="row" ng-repeat="wallet in wallets">
<div class="col item item-input-inset">
<label class="item-input-wrapper item-text-wrap">
<input name="wallet_{{wallet.id}}" type="number" ng-model="wallet.value" type="text" required/>
</label>
<span ng-show="innerForm['wallet_'+wallet.id].$error.required">!!!</span>
</div>
<div class="col item">{{ wallet.previous }}</div>
<button ng-click="sendValue(wallet)">
<i class="ion-android-send"></i>
</button>
<span class=ng-show="innerForm.$submitted==true && innerForm['wallet_'+wallet.id].$error.required">Required</span>
</div>
<button class="button" type="submit">Submit</button>
</form>

How to horizontally position elements for a pagination toolbar using Boostrap?

I'm working on an AngularJS project and in one of my views I have a table that will have pagination implemented on it. I want the controls for the pagination to be as follows:
Stacked horizontally, to have a Previous button, a text indicating the page of total of pages, and a Next button.
A select containing the number of elements per page desired to visualize on the table, and a text just saying "elements per page"
A text input to enter the number of page, and a Go to button that will load the indicated page.
All of these have to be sorted horizontally.
I defined this components like this in my view:
<div class="container">
<div class="row">
<div class="col-md-6">
<ul class="pagination pagination-sm">
<li >Anterior</li>
<li ><a>1 de 10 páginas</a></li>
<li>Siguiente</li>
</ul>
</div>
<div class="col-md-3">
<select class="form-control" ng-model="uc.defaultSelect" ng-options="n for n in uc.elementsPerPage" ></select>
<label>elementos por página</label>
</div>
<div class="col-md-3">
<div class="input-group">
<input type="text" class="form-control" placeholder="Ir a página..." />
<span class="input-group-btn">
<button class="btn btn-default" type="button">Ir</button>
</span>
</div>
</div>
</div>
</div>
But the end result I'm getting looks like this
The nav buttons look completely off line, the select is too big and it's pushing the label to a new line, the one that looks fine is the go to page input.
What kind of styling can I use with Bootstrap to achieve the look I desire?
In the end I had to give up on using the paginationstyle and changed to button-group, I removed some labels and ended up with this:
<div class="container">
<div class="row">
<div class="col-md-6 btn-group">
<button type="button" class="btn btn-default" ng-click="uc.previousPage()">Anterior</button>
<a class="btn btn-default disabled">1 de 10 páginas</a>
<button type="button" class="btn btn-default" ng-click="uc.nextPage()">Siguiente</button>
</div>
<div class="col-md-3">
<select class="form-control form-control-sm" ng-model="uc.defaultSelect" ng-options="n as (n + ' por página') for n in uc.elementsPerPage" ></select>
</div>
<div class="col-md-3">
<div class="input-group">
<input type="text" ng-model="uc.page" class="form-control" placeholder="Ir a página..." />
<span class="input-group-btn">
<button class="btn btn-default" type="button" ng-click="uc.goToPage()">Ir</button>
</span>
</div>
</div>
</div>
</div>
Now I get this in my view:
I can just add some color to it if as necessary and I just got a pretty looking pagination toolbar.

Issue with angular radio input not populating the model when clicked

I have a strange issue with a radio input not populating the model of an angular form.
All fields populate the model except for the radio input...
The radio input is given at the beginning of the form below. I use ng-inspector and I can see that signupForm.member.role does not get populated when I click one of the radio values.
Can someone please help?
<form name="formCtrl" ng-submit="signup(formCtrl)" class="col-xs-12" novalidate role="form">
<h4>{{'SIGNUP_FORM_ROLE_PREFIX' | translate}}</h4>
<div class="btn-group Choix col-xs-12 text-center" data-toggle="buttons" ng-class="getCssClasses(formCtrl, formCtrl.signupRole)">
<label class="btn StateButton col-xs-6">
<img class="img-responsive" src="assets/media/img/parents.svg" />
<input type="radio" name="signupRole" ng-model="signupForm.member.role" value="ROLE_BASIC_PARENTS" ng-required="true" />
<span class="help-block">{{'DOMAIN_ENUM_' + 'ROLE_BASIC_PARENTS' | translate}}</span>
</label>
<label class="btn StateButton col-xs-6">
<img class="img-responsive" src="assets/media/img/professionel.svg" />
<input type="radio" name="signupRole" ng-model="signupForm.member.role" value="ROLE_BASIC_CHILDCARE_WORKER" ng-required="true" />
<span class="help-block">{{'DOMAIN_ENUM_' + 'ROLE_BASIC_CHILDCARE_WORKER' | translate}}</span>
</label>
<div ng-messages="formCtrl.signupRole.$error" ng-if="formCtrl.$submitted">
<div ng-message="required" class="control-label">{{'SIGNUP_FORM_ROLE_REQUIRED'| translate}}</div>
</div>
</div>
<hr>
<div class="form-group" ng-class="getCssClasses(formCtrl, formCtrl.firstName)">
<div class="input-group">
<span class="input-group-addon">
<span class="glyphicon icon-Prenom" aria-hidden="true"></span>
</span>
<input type="text" name="firstName" ng-minlength="2" placeholder="{{'SIGNUP_FORM_FIRST_NAME' | translate}}" ng-model="signupForm.member.firstName" ng-required="true" class="form-control" />
<span ng-if="isSuccessFeedback(formCtrl, formCtrl.firstName)" class="form-control-feedback" aria-hidden="true"><span class="glyphicon icon-Valid" aria-hidden="true"></span></span>
<span ng-if="isErrorFeedback(formCtrl, formCtrl.firstName)" class="form-control-feedback" aria-hidden="true"><span class="glyphicon icon-Erreur" aria-hidden="true"></span></span>
</div>
<div ng-messages="formCtrl.firstName.$error" ng-if="formCtrl.$submitted">
<div ng-message="required" class="control-label">{{'SIGNUP_FORM_FIRST_NAME_REQUIRED' | translate}}</div>
<div ng-message="minlength" class="control-label">{{'SIGNUP_FORM_FIRST_NAME_REQUIRED' | translate }}</div>
</div>
</div>
</form>
Here is a working plunker: http://plnkr.co/edit/lsaevbL0mBRku7zcrGDJ
One can notice that the radio is not taken into account when the form is submitted even if one of the radios is selected...
Simply use value instead of ng-value. Please see this working demo: http://plnkr.co/edit/2EB4NvTO1StN50NIXOyn?p=preview.
You should not use ng-value, which is looking for $scope.ROLE_BASIC_PARENTS.
Check AngularJS.org, the example there use:
<input type="radio" ng-model="color.name" ng-value="specialValue">
And in the controller:
$scope.specialValue = {
"id": "12345",
"value": "green"
};

Resources