ngClick for only "this" element in list | AngularJS - angularjs

I have a list of users where by if the user clicks on a "edit" icon next to each person name, they have options appear.
I'm wanting to make it so that the user can only edit one person at a time and not edit lets say, two or more people at a time.
Example: If the user selects edit icon, open edit options. If user selects another persons edit icon, close current icon and show the users next selected edit options.
I'm using to toggle the views:
ng-click="editDetails = ! editDetails"
ng-class="{ 'unseen': ! editDetails }
Here is my code:
<div class="col-xs-12 col-md-6 col-md-push-6">
<div ng-show="memberNeeded" class="message">Please add hunt group members</div>
<h4>Hunt group call order</h4>
<div class="list-box">
<ul id="finalList" ng-class="{disable: details.disabled}">
<li ng-repeat="item in list" switch data-id="{{item.id}}" data-active="{{item.isActive}}" class="clearfix {{control}}">
<strong>{{item.firstName}} {{item.lastName}}</strong>
<i class="vf-i vf-i-pen pull-right" ng-click="editDetails = ! editDetails"></i>
<div class="list-panel clearfix unseen" ng-class="{ 'unseen': ! editDetails }">
<p class="pull-left">
Active
</p>
<div class="pull-left">
<div class="toggle-bg switch-toggle {{control}}">
<label class="{{control}}">{{control}}</label>
<input type="radio" name="toggle" value="off" ng-model="control" ng-click="updateUser($index, 'off')">
<input type="radio" name="toggle" value="on" ng-model="control" ng-click="updateUser($index, 'on')">
<span class="switch {{control}}"></span>
</div>
</div>
<button class="btn btn-del pull-right">Remove</button>
</div>
</li>
</ul>
</div>
</div>

If you only want one editor active at a time, just store the active item rather than a flag. On ng-click set the activeItem. Example below:
In your controller:
$scope.editor = {
activeEditItem: null
};
and your markup becomes:
<div class="col-xs-12 col-md-6 col-md-push-6">
<div ng-show="memberNeeded" class="message">Please add hunt group members</div>
<h4>Hunt group call order</h4>
<div class="list-box">
<ul id="finalList" ng-class="{disable: details.disabled}">
<li ng-repeat="item in list" switch data-id="{{item.id}}" data-active="{{item.isActive}}" class="clearfix {{control}}">
<strong>{{item.firstName}} {{item.lastName}}</strong>
<i class="vf-i vf-i-pen pull-right" ng-click="editor.activeEditItem === item ? editor.activeEditItem = null : editor.activeEditItem = item"></i>
<div class="list-panel clearfix unseen" ng-class="{ 'unseen': editor.activeEditItem !== item }">
<p class="pull-left">
Active
</p>
<div class="pull-left">
<div class="toggle-bg switch-toggle {{control}}">
<label class="{{control}}">{{control}}</label>
<input type="radio" name="toggle" value="off" ng-model="control" ng-click="updateUser($index, 'off')">
<input type="radio" name="toggle" value="on" ng-model="control" ng-click="updateUser($index, 'on')">
<span class="switch {{control}}"></span>
</div>
</div>
<button class="btn btn-del pull-right">Remove</button>
</div>
</li>
</ul>
</div>
</div>

It won't work as it will toggle all your classes.
What I would do if I were you:
In your "item" object add a variable "editActive" set it by default to false
Change your ng-class="..." by ng-show="item.editActive"
In your controller create a variable $scope.currentActive
Create a function in your controller that will be triggered by the ng-click which will get the currentActive, if there is one then set its editActive value to false. Then set the new currentActive value and set its editActive to true
It should work I think.

Related

Display button only when option is selected

I want my button to display only when one of the options is selected in the select area.
<div class="row" ng-controller='ctrl1'>
<div class ="form_group">
<select class="form-control" ng-options='item as item.hotel_name for item in hotels.data' ng-model='current_Hotel' ng-click="selectHotel()"><option value="" selected disabled>Choose a hotel</option>
</select>
</div>
<div class="col-md-6">
<h1>{{current_Hotel.hotel_name}}</h1>
<p>{{current_Hotel.hotel_description}}</p>
<img class="img img-responsive" ng-src="{{current_Hotel.image_url}}">
<btn class="btn btn-primary">Book a room </btn>
</div>
</div>
I tried using ng-show and ng-hide but it didn't worked as I wanted.
use ng-if directive with multiple conditions
<div class="col-md-6" ng-if="current_Hotel && current_Hotel != ''">
<h1>{{current_Hotel.hotel_name}}</h1>
<p>{{current_Hotel.hotel_description}}</p>
<img class="img img-responsive" ng-src="{{current_Hotel.image_url}}">
<btn class="btn btn-primary">Book a room </btn>
</div>
ng-show or ng-hide should work based on the value of the ng-model that you have assigned when an option is choosen, supposing that you are working in the same scope.
Have you tried ng-show="current_Hotel !== ''"?
ngmodel for select will be set when option is selected, you need to check if model is selected or not
<div class="row" ng-controller='ctrl1'>
<div class ="form_group">
<select class="form-control" ng-options='item as item.hotel_name for item in hotels.data' ng-model='current_Hotel' ng-click="selectHotel()"><option value="" selected disabled>Choose a hotel</option>
</select>
</div>
<div class="col-md-6">
<h1>{{current_Hotel.hotel_name}}</h1>
<p>{{current_Hotel.hotel_description}}</p>
<img class="img img-responsive" ng-src="{{current_Hotel.image_url}}">
<btn ng-show="current_Hotel" class="btn btn-primary">Book a room </btn>
</div>
</div>

can't get data from ng-model using signalr

here is my .html :
<ul class="messages list-unstyled" ng-repeat="newMessage in messages">
<li>
<p>
<span class="username">{{newMessage.username}}</span><br />
</p>
<p>
<span> {{newMessage.message}}</span>
</p>
<p class="no-pad-bottom date-posted">Send {{ newMessage.date }} <span /></p>
<div class="comments">
<ul class="list-unstyled" ng-repeat="newComment in comments">
<li>
<p>
<span class="commentor"> {{newComment.username}}</span>
<span> {{newComment.message}}</span>
</p>
<p class="no-pad-bottom date-posted">Send {{ newComment.date }} <span /></p>
</li>
</ul>
<form class="add-comment">
<div class="row">
<div class="col-md-10">
<input type="text" class="form-control" ng-model="myComment" placeholder="Add a comment" />
</div>
<div class="col-md-2">
<button class="btn btn-default btn-sm" type="submit" ng-click="addComment(newMessage.id)">Comment</button>
</div>
</div>
</form>
</div>
</li>
</ul>
everything is working. i can send my messages, and i can even send posts, but i get null from ng-model="myComment"
here is the method in js controller
$scope.addComment = function (messageId) {
myChatHub.server.addComment(messageId, userName, $scope.myComment);
};
can you please tell me what's wrong?
You are breaking the golden rule of always having a dot in ng-model .. or in other words using an object
ng-repeat creates a child scope and your primitive assigned to ng-model therefore only exists in the child scope since primitives have no inheritance.
Create model object in controller
$scope.model={}
And then use that ng-model="model.myComment" and change your post to:
$scope.addComment = function (messageId) {
myChatHub.server.addComment(messageId, userName, $scope.model.myComment);
};
This 3 minute video will be very enlightening

Angular JS ng-show/ hide based on drop-down list selected value

in the following code I have one drop-down list (serviceSmallId) for type of service.
It is populated using model info.
There is a second field check-box which should only be visible when drop-down has a selected value of 'Weekly'. I am trying to use ng-show/ hide of angular.
I tried to search for possible solutions but no luck.
Can anyone please guide me what I am doing wrong.
<section id="scrubber-view" class="mainbar" data-ng-controller="scrubber as vm">
<section class="matter">
<div class="container">
<div>
<button class="btn btn-info" ng-click="vm.goBack()"><i class="fa fa-arrow-left"></i>Back</button>
<button class="btn btn-info" ng-click="vm.cancel()" ng-disabled="!vm.canSave"><i class="fa fa-undo"></i>Cancel</button>
<button class="btn btn-info" ng-click="vm.save()" ng-disabled="!vm.canSave"><i class="fa fa-save"></i>Save</button>
<span ng-show="vm.hasChanges" style="color:orange" class="dissolve-animation ng-hide"><i class="fa fa-asterisk"></i></span>
</div>
<div class="row">
<div class="widget wblue">
<div data-cc-widget-header title="{{vm.title}}" subtitle="{{vm.scrubber.scrubberId}}"></div>
<form class="form-horizontal">
<div class="form-group">
<label for="serviceSmallId" class="col-sm-2">Service</label>
<div class="col-sm-4">
<select class="form-control" id="serviceSmallId" ng-model="vm.scrubber.serviceSmallId"
ng-options="item.dataLookupId as item.description for item in vm.serviceSmalls | orderBy:['sortOrder','description']">
</select>
</div>
</div>
<div class="form-group" ng-show="vm.scrubber.serviceSmallId.value=='Weekly'">
<input class="form-control" type="checkbox" id="fullyFlanged" value="Bar" />
</div>
</form>
</div>
</div>
</div>
</section>
</section>
There probably is a more elegant "angular way" solution,
but I updated you code to work here - http://jsbin.com/tupisoriho/1/edit?html,js,output
Explanation of changes
Provided ng-show a value vm.checker
added ng-change to the select element and gave it a function checkerGo which tested to see if dropdown == 'weekly', if yes change vm.checker
Update
there is a more "angular way" - using expressions!
As per #Omri Aharon fiddle/comment below.
You don't need to get the value property because a ng-model don't hold the element but the value itself;
<div class="form-group" ng-show="vm.scrubber.serviceSmallId.value=='Weekly'">
must be
<div class="form-group" ng-show="vm.scrubber.serviceSmallId == 'Weekly'">
EDIT: In your case vm.scrubber.serviceSmallId will contain the ID and not the description Weekly. I suggest you to use ng-change directive to call a function in your controller that will find the item based on ID and set in the controller to be visible for ng-show.
<select class="form-control" id="serviceSmallId" ng-model="vm.scrubber.serviceSmallId"
ng-options="item.dataLookupId as item.description for item in vm.serviceSmalls | orderBy:['sortOrder','description']"
ng-change="vm.selectObj()">
vm.selectObj() will find and set the current selected object in the scope to an controller variable (ex.: vm.selectedItem) and:
<div class="form-group" ng-show="vm.selectedItem.description=='Weekly'">

show and hide div from a repeat

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>

In AngularJS, how do I set the selected option from select as a parameter elsewhere on the page?

In AngularJS, how do I get the selected option from a select element to put it somewhere else on the page?
<div class="modal-body">
<form class="">
<div class="control-group">
<label class="control-label" for="role">Choose a Playlist</label>
<div class="controls">
<select name="playlist">
<option ng-repeat="playlist in playlists | orderBy: 'playlist'" value="{{playlist.id}}">{{ playlist.name }}</option>
</select>
</div>
</div>
<p>
Or,
<a class="branded" href="#">Create New Playlist</a>
</p>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-branded" ng-click="postSongToPlaylist();">Save</button>
</div>
I want to get the option the user selects in the select element and put it as the parameter for the ng-click. How do I get Angular to "know" which option is the right one?
The value will be stored in the scope as the name you give to ng-model for the select tag.
<div class="modal-body">
<form class="">
<div class="control-group">
<label class="control-label" for="role">Choose a Playlist</label>
<div class="controls">
<select name="playlist" ng-model="playlist" ng-options="playlist.id as playlist.name for playlist in playlists | orderBy: 'playlist'">
</select>
</div>
</div>
<p>
Or,
<a class="branded" href="#">Create New Playlist</a>
</p>
</form>
</div>
<div class="modal-footer">
<button class="btn btn-branded" ng-click="postSongToPlaylist(playlist);">Save</button>
</div>
I would also suggest that you use ng-options instead of ng-repeat to populate your option tags.

Resources