I am building an application with angular where I have a list of items (using ng-repeat), and by clicking on each item I can open a modal in order to see a more detailed description.
Right now in order to switch to another modal I have to close the previous one, go to the list, and click to open another modal. I would like to go directly to the previous/next modal when clicking the previous/next button. I have made an example of what I want to achieve.
Here is the html:
<div ng-app="myApp" ng-controller="myCtrl">
<ul>
<li ng-repeat="t in turtles">
{{t.name}} - {{t.weapon}}
<a ng-click="show = !show">Show more</a>
<div class="modal" ng-show="show">
<div class="close" ng-click="show = !show">X</div>
{{t.name}} likes to eat {{t.food}} with his {{t.weapon}}!
<div class="previous">Previous</div>
<div class="next">Next</div>
</div>
</li>
</ul>
</div>
And the controller:
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
$scope.turtles = [
{ name: "Michellangelo", weapon: "nunchaku", food:"pizza" },
{ name: "Donatello", weapon: "bo", food:"pizza" },
{ name: "Leonardo", weapon: "katana", food:"turtle soup" },
{ name: "Rafael", weapon: "sai", food:"pizza" }
];
$scope.show=false;
});
You can see a working Jsfiddle here
Here is a working solution: http://jsfiddle.net/s7gc81zz/5/
Instead of relying on a single variable to show or hide the modals, you can add a show attribute to each turtle, and use the $index variable of ngRepeat to reference your position in the array:
<div ng-app="myApp" ng-controller="myCtrl">
<ul>
<li ng-repeat="t in turtles">
{{t.name}} - {{t.weapon}}
<a ng-click="showMore(t)">Show more</a>
<div class="modal" ng-show="t.show">
<div class="close" ng-click="hideMore(t)">X</div>
{{t.name}} likes to eat {{t.food}} with his {{t.weapon}}!
<div class="previous" ng-click="showPrevious(t, $index)>Previous</div>
<div class="next" ng-click="showNext(t, $index)">Next</div>
</div>
</li>
</ul>
</div>
Now you use scope functions to handle the logic of the next button:
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
$scope.turtles = [
{ name: "Michellangelo", weapon: "nunchaku", food:"pizza" },
{ name: "Donatello", weapon: "bo", food:"pizza" },
{ name: "Leonardo", weapon: "katana", food:"turtle soup" },
{ name: "Rafael", weapon: "sai", food:"pizza" }
];
//$scope.show=false;
$scope.showMore = function(turtle) {
turtle.show = true;
};
$scope.hideMore = function(turtle) {
turtle.show = false;
};
$scope.showNext = function(turtle, index) {
if((index+1) > ($scope.turtles.length - 1)) {
return;
}
else {
turtle.show = false;
$scope.turtles[index+1].show = true;
}
};
$scope.showPrevious = function(turtle, index) {
if((index-1) < 0) {
return;
}
else {
turtle.show = false;
$scope.turtles[index-1].show = true;
}
};
});
If you want to create a navigation in the modal from next and previous buttons, then instead of having one show flag, you should have a flag on each object of the displaying array i.e turtles. And based on current looping object of ng-repeat you can then hide/ show appropriate item on next and previous buttons based on $index +1 & -1 respectively.
Here is the working Fiddle Code :
http://jsfiddle.net/ashwani121/4fa6a9pr/
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
$scope.turtles = [
{ name: "Michellangelo", weapon: "nunchaku", food:"pizza" },
{ name: "Donatello", weapon: "bo", food:"pizza" },
{ name: "Leonardo", weapon: "katana", food:"turtle soup" },
{ name: "Rafael", weapon: "sai", food:"pizza" }
];
$scope.show=false;
var prevObj = null;
$scope.showModal = function(obj){
if(prevObj && prevObj.selected)
{
prevObj.selected = false;
}
obj['selected'] = true;
prevObj = obj;
}
$scope.navModal = function(index){
if(index > 0 && index < $scope.turtles.length)
$scope.showModal($scope.turtles[index]);
}
});
<div ng-app="myApp" ng-controller="myCtrl">
<ul>
<li ng-repeat="t in turtles">
{{t.name}} - {{t.weapon}}
<a ng-click="showModal(t)">Show more</a>
<div class="modal" ng-show="t.selected">
<div class="close" ng-click="show = !show">X</div>
{{t.name}} likes to eat {{t.food}} with his {{t.weapon}}!
<div class="previous" ng-click="navModal($index-1)">Previous</div>
<div class="next" ng-click="navModal($index+1)">Next</div>
</div>
</li>
</ul>
</div>
You can use an index variable to keep track of the ninja turtle you have to show currently.
BTW you are creating unnecessary model/popup's because you only need one and you don't need a model for each of the ninja turtle :) so it is better to have only one model popup.
You can move to the next and previous ninja turtle by using the index variable you will be storing and updating it accordingly on the $scope object.
Check the code below, here the updated jsfiddle
<div ng-app="myApp" ng-controller="myCtrl">
<ul>
<li ng-repeat="t in turtles">
{{t.name}} - {{t.weapon}}
<a ng-click="showPopup($index)">Show more</a>
</li>
</ul>
<div class="modal" ng-show="show">
<div class="close" ng-click="closePopup()">X</div>
{{selectedTurtle.name}} likes to eat {{selectedTurtle.food}} with his {{selectedTurtle.weapon}}!
<div class="previous" ng-click="prev()">Previous</div>
<div class="next" ng-click="next()">Next</div>
</div>
</div>
angular
.module('myApp', [])
.controller('myCtrl', ['$scope', function ($scope) {
$scope.index = 0;
$scope.selectedTurtle = {};
$scope.turtles = [
{ name: "Michellangelo", weapon: "nunchaku", food:"pizza" },
{ name: "Donatello", weapon: "bo", food:"pizza" },
{ name: "Leonardo", weapon: "katana", food:"turtle soup" },
{ name: "Rafael", weapon: "sai", food:"pizza" }
];
$scope.show = false;
$scope.showPopup = showPopup;
$scope.closePopup = closePopup;
$scope.next = next;
$scope.prev = prev;
function setTurtle(index) {
$scope.selectedTurtle = $scope.turtles[index];
}
function showPopup(index) {
$scope.index = index;
setTurtle($scope.index);
$scope.show = true;
}
function closePopup() {
$scope.show = false;
}
function next() {
if ($scope.index !== $scope.turtles.length - 1) {
$scope.index++;
setTurtle($scope.index);
}
}
function prev() {
if ($scope.index !== 0) {
$scope.index--;
setTurtle($scope.index);
}
}
}]);
as you maybe already noticed ng-show just add/remove a css class. So based on this, you should give an ID to your DIVs, maybe using the internal var $index from the ng-repeat directive, for example id="card-{{$index}}". Having identified your DIVs and with a little jquery help (it also can be done with just plain js) you can identify the active card, check its ID, calculate the next or previous ID value, and add or remove any particular css class you need.
Or you can just look for an angular carousel ;)
Related
I use a directive to add images and radio buttons (per image). I also create dynamically ng-models. Now with my approach, the radio buttons will not be set. What's wrong? Thank your for your hints.
JS
.directive('oct', function() {
return {
restrict : 'E',
template : `
<div class="oImg" ng-repeat="image in images">
<a href="#" class="removeImage" ng-click="vm.removeImg(image)"
ng-show="isActive($index)"><i class="fa fa-trash"></i>
</a>
<img ng-src="{$image.src$}" class="images-item"
ng-show="isActive($index)"><span ng-show="isActive($index)" class="imgName">{$image.name$}</span>
<div class="sideSelect" ng-show="isActive($index)">
<div class="checkbox"><label>
<input type="radio" ng-model="eyeChanger[image.name]"
name="optionsEye"
ng-change="vm.changeEye(image, eyeChanger[image.name])"
value="RA">RA</label>
</div>
<div class="checkbox"><label>
<input type="radio" ng-model="eyeChanger[image.name]"
ng-change="vm.changeEye(image, eyeChanger[image.name])"
name="optionsEye" value="LA">LA</label>
</div>
</div>
</div>
`,
controller : ['$scope', '$http',
function($scope, $http) {
$scope._Index = 0;
// if current image is same as requested image
$scope.isActive = function(index) {
return $scope._Index === index;
};
// prev image
$scope.showPrev = function() {
if ($scope._Index != 0) {
$scope._Index = ($scope._Index > 0) ? --$scope._Index : $scope.images.length - 1;
}
};
// next image
$scope.showNext = function() {
if ($scope._Index < $scope.images.length - 1) {
$scope._Index = ($scope._Index < $scope.images.length - 1) ? ++$scope._Index : 0;
};
};
}]
};
})
vm.changeEye = function(img, value) {
$scope['eyeChanger'+img.name] = value;
...
Use:
vm.changeEye = function(img, value) {
̶$̶s̶c̶o̶p̶e̶[̶'̶e̶y̶e̶C̶h̶a̶n̶g̶e̶r̶'̶+̶i̶m̶g̶.̶n̶a̶m̶e̶]̶ ̶=̶ ̶v̶a̶l̶u̶e̶;̶
$scope.eyeChanger[img.name] = value;
...
There is no need to do this with ng-change as the ng-model directive does it automatically.
<div ng-repeat="image in images">
<img ng-src="{{image.src}}" />
<div class="checkbox"><label>
<input type="radio" ng-model="eyeChanger[image.name]"
name="optionsEye"
̶n̶g̶-̶c̶h̶a̶n̶g̶e̶=̶"̶v̶m̶.̶c̶h̶a̶n̶g̶e̶E̶y̶e̶(̶i̶m̶a̶g̶e̶,̶ ̶e̶y̶e̶C̶h̶a̶n̶g̶e̶r̶[̶i̶m̶a̶g̶e̶.̶n̶a̶m̶e̶]̶)̶"̶
value="RA">RA</label>
</div>
<div class="checkbox"><label>
<input type="radio" ng-model="eyeChanger[image.name]"
̶n̶g̶-̶c̶h̶a̶n̶g̶e̶=̶"̶v̶m̶.̶c̶h̶a̶n̶g̶e̶E̶y̶e̶(̶i̶m̶a̶g̶e̶,̶ ̶e̶y̶e̶C̶h̶a̶n̶g̶e̶r̶[̶i̶m̶a̶g̶e̶.̶n̶a̶m̶e̶]̶)̶"̶
name="optionsEye" value="LA">LA</label>
</div>
</div>
The radio buttons can be initialized:
$scope.images = [
{ name: "name1", src: "..." },
{ name: "name2", src: "..." },
// ...
];
$scope.eyeChanger = {
name1: "RA",
name2: "LA",
// ...
};
So the thing is:
I have four cards and I have a ng-click on the first one with a function named OpenCard().
When I click, it shows its hidden content.
I wanna do the same for the rest of the cards, using the same call to OpenCard().
My four classes have the same name "rowCont" and the hidden content inside that "rowCont" is different:
<div class="rowCont" ng-click="OpenCard()" ng-class="{'active': isActive}">
<div class="hiddenContent">
<div class="animate-show" ng-show="cardVisible">
</div>
</div>
</div>
$scope.isActive = false;
$scope.OpenCard = function () {
if($scope.isActive == false) {
$scope.isActive = true;
$scope.cardVisible = true;
} else {
$scope.isActive = false;
$scope.cardVisible = false;
}
}
I'm using Angular 1.6.0
Do you have an idea how can I refer to one card in specific using the same function on ng-click? Cause when I click one in one closed card it shows the content of all cards.
<div class="row">
ng-repeat="x in current_tab
ng-class="{active1 : activeMenu === x}"
ng-click="setActive(x);"> {{x.userId}}
</div>
$scope.menuItems = $rootScope.current_tab;
$scope.activeMenu = $scope.menuItems[0];
$scope.setActive = function(menuItem) {
$scope.activeMenu = menuItem
}
var app = angular.module("ngClickApp", []);
app.controller('ngClickCtrl', ['$scope', function ($scope) {
$scope.cards = [{
isActive: false,
title: '1',
content: 'content 1'
}, {
isActive: false,
title: '2',
content: 'content 2'
}];
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app="ngClickApp">
<head>
<title>Validation messages</title>
</head>
<body ng-controller="ngClickCtrl">
<div class="rowCont" ng-repeat="card in cards track by $index" ng-click="card.isActive=!card.isActive" ng-class="{'active': c.isActive}">
card {{$index}}
<div class="hiddenContent">
<div class="animate-show" ng-show="card.isActive">
{{card.content}}
</div>
</div>
</div>
</body>
</html>
You can store card's visibility in an array ($scope.cardsVisible = [];), and pass an index in each call to OpenCard(cardIndex).
Then, display it conditionnaly in your view:
var myApp = angular.module('myApp', []);
function MyCtrl($scope) {
$scope.cardsVisible = [];
$scope.OpenCard = function(cardIndex) {
$scope.cardsVisible[cardIndex] = true;
$scope.isActive = cardIndex;
}
}
.active {
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<div ng-click="OpenCard(1)" ng-class="{'active': isActive == 1}">
Card 1
<div ng-show="cardsVisible[1]">
This card is visible
</div>
</div>
<div ng-click="OpenCard(2)" ng-class="{'active': isActive == 2}">
Card 2
<div ng-show="cardsVisible[2]">
This card is visible
</div>
</div>
</div>
I would like use isOpen attributes in angualr ui bootstrap accordion directive so it would open the first element of the first ng-repeat in the accordion. I have tried few things with no luck. can anyone advise on this?
//html
<div ng-show="accordionCtrl.isNotString(value);" class="accordionContainer" ng-repeat="(key, value) in accordionCtrl.lessons">
<div class="accordionStepBox">
<h4 class="accordionStepTitle">Step {{$index+1}}: {{value.title}}</h4>
<span>Summary: {{value.summary}}</span>
</div>
<div class="accordionCoursesBox">
<div class="accordionCoursesText">Courses</div>
<!-- accordion for suffles -->
<uib-accordion close-others="accordionCtrl.oneAtATime">
<!-- only give accordion to object vals -->
<div class="accordion" ng-show="accordionCtrl.isNotString(value);" ng-repeat="(key, value) in value">
<!-- <uib-accordion-group heading="{{value.title}}" is-open="accordionCtrl.firstIndex($index)"> -->
<uib-accordion-group heading="{{value.title}}">
<div ng-repeat="(key, value) in value">
<div ng-show="accordionCtrl.isNotString(value);" class="accordionSuffleBox">
{{$index+1}}. {{value.title}}
</div>
</div>
<br/>
<button ui-sref="lesson" class="btn btn-default accordionbutton">Start</button>
</uib-accordion-group>
</div>
</uib-accordion>
</div>
</div>
//controller
angular
.module('neuralquestApp')
.controller('AccordionCtrl', AccordionCtrl);
function AccordionCtrl(FirebaseUrl, $firebaseObject, $firebaseArray) {
var accordionCtrl = this;
var getLessons = getLessons;
accordionCtrl.oneAtATime = true;
accordionCtrl.init = init;
accordionCtrl.init();
accordionCtrl.isNotString = isNotString;
accordionCtrl.firstIndex = firstIndex;
/*======================================
= IMPLEMENTATION =
======================================*/
function init() {
getLessons();
}
function firstIndex(index) {
if(index === 0){
return true;
} else {
return false;
}
}
function getLessons() {
var ref = new Firebase(FirebaseUrl);
accordionCtrl.lessons = $firebaseObject(ref.child('NeuralNetwork'));
}
function isNotString(val) {
// console.log('val', val);
if(typeof val === "string"){
console.log('is string', val);
return false;
} else {
return true;
}
}
}
The is-open attribute is setup for 2 way binding with the controller, so you could do something like so:
<div ng-controller="AccordionDemoCtrl">
<uib-accordion>
<div ng-repeat="group in groups">
<uib-accordion-group heading="{{group.title}}" is-open="openIndex[$index]">
{{group.content}}
</uib-accordion-group>
</div>
</uib-accordion>
</div>
angular.module('ui.bootstrap.demo').controller('AccordionDemoCtrl', function ($scope) {
$scope.openIndex = [true];
$scope.groups = [
{
title: 'Group Header - 1',
content: 'Group Body - 1'
},
{
title: 'Group Header - 2',
content: 'Group Body - 2'
},
{
title: 'Group Header - 3',
content: 'Group Body - 3'
},
{
title: 'Group Header - 4',
content: 'Group Body - 4'
}
];
});
Example plunk. Also, the close-others attribute default value is true, so you don't need to explicitly set that to true.
I have a simple array of objects {name,age} (see my jsfiddle). I use an ng-repeat to print all the names and I would like to print, outside the ng-repeat loop, the age of the name on which I click.
<body ng-app="myapp">
<div ng-controller="MyController" >
<ul>
<li ng-repeat="friend in myData.friends" ng-click="myData.doClick($index)">{{friend.name}}</li>
</ul>
<p>Age :</p>
<p>{{}}</p>
</div>
<script>
angular.module("myapp", []).controller("MyController", function($scope) {
$scope.myData = {};
$scope.myData.friends = [{ name: "Al",age:26}, { name: "Mike",age:21}, { name: "Brian",age:46} ];
$scope.myData.doClick = function(item) {
}
} );
</script>
</body>
Instead of passing the $index, you should pass the object itself, friend, then you can read his information.
Quick refactoring: http://jsfiddle.net/par2nrd4/4/
The below code should work for you....
<body ng-app="myapp">
<div ng-controller="MyController" >
<ul>
<li ng-repeat="friend in myData.friends" ng-click="myData.doClick(friend.name)">{{friend.name}}</li>
</ul>
<p>Age :</p>
<p>{{ageval}}</p>
</div>
<script>
angular.module("myapp", []).controller("MyController", function($scope) {
$scope.myData = {};
$scope.myData.friends = [{ name: "Al",age:26}, { name: "Mike",age:21}, { name: "Brian",age:46} ];
$scope.myData.doClick = function(item) {
for (i = 0; i < $scope.myData.friends.length; i++) {
if (item == $scope.myData.friends[i]["name"]) {
$scope.ageval = $scope.myData.friends[i]["age"];
}
}
}
} );
</script>
</body>
I want to store the users vote's inside a cookie, the problem is that inside the ng-repeat I have a value called session.upVoteCount. But it is supposed to be a separate value for each event list item. Is it possible to store each upVoteCount separately and then retrieve them separately again?
<li ng-repeat="session in event.sessions | filter:query | orderBy:sortorder" class="span11">
<div class="row session">
<div class="col-sm-1 well votingWidget">
<div class="votingButton" ng-click="upVoteSession(session)">
<span class="glyphicon glyphicon-arrow-up"></span>
</div>
<div class="badge badge-inverse">
<div>{{session.upVoteCount}}</div>
</div>
<div class="votingButton" ng-click="downVoteSession(session)">
<span class="glyphicon glyphicon-arrow-down"></span>
</div>
</div>
</div>
</li>
and in my controller I have this:
$scope.upVoteSession = function(session) {
session.upVoteCount++;
};
$scope.downVoteSession = function(session) {
session.upVoteCount--;
};
First, I don't recommend to use term 'session', but 'votes'. However, it's your call.
I simplify your problem in this example
http://plnkr.co/edit/l7tQRbuOtEDJetY5eTsf?p=preview
Javascript:
function MyCtrl($scope) {
$scope.votes = {};
$scope.vote = function(key, val) {
$scope.votes[key] = $scope.votes[key] || 0;
$scope.votes[key]+= val;
};
}
Html:
<li ng-repeat="no in [1,2,3,4,5]">
{{no}} : {{votes[no]}} <br/>
upvote
downvote
</li>
Hi guys I solved it myself, I could not get it to work with JSfiddle so I have uploaded the entire thing. Click on server.bat and browser to localhost:8000/eventdetails.html and you will see it working.
https://mega.co.nz/#!1d9yiYiA!zTzdztLAmhVDVYOvvVLINETI2bo_WjxCBteWYm2VUKc
controller:
eventsApp.controller('EventController',
function EventController($scope, $cookieStore, eventData) {
$scope.sortorder = 'name';
var ape = eventData.getEvent();
ape.then(function (banana) {
$scope.event = banana;
angular.forEach(banana.sessions, function (value, key) {
var storecookie = ($cookieStore.get(value.name));
if (typeof storecookie !== "undefined") {
value.upVoteCount = storecookie;
}
});
});
$scope.upVoteSession = function (session) {
session.upVoteCount++;
$cookieStore.put(session.name, session.upVoteCount);
};
$scope.downVoteSession = function (session) {
session.upVoteCount--;
$cookieStore.put(session.name, session.upVoteCount);
};
}
);