AngularJS, start ng-repeat from particular object in array of objects - angularjs

I have a AngularJS controller in which there is an array of objects. I want to start iterating from a particular object in this array which has a particular key:value pair. Also iterations must be limited to only 4.
Here is the controller code:
app.controller("products", ["$scope", function($scope) {
$scope.products = [
{
code: 1234,
icon: "../product/images/abc.jpeg",
name: "One",
description: "lorem ipsum sil vous plait 1",
link:"a.php",
price: 100,
stock: ""
},
{
code: 1235,
icon: "../product/image/def.jpeg",
name: "Two",
description: "lorem ipsum sil vous plait 2",
link:"b.php",
price: 200,
stock: ""
},
// more such objects
];
}]);
Here is the HTML file for <products> tag:
<div class="card">
<div class="card-image">
<img ng-src="{{ info.icon }}">
</div>
<div class="card-content">
<p>{{ info.name }}</p>
<p>{{ info.price }}</p>
</div>
</div>
Here is the HTML:
<div ng-controller="products">
<div ng-repeat="product in products | limitTo:4">
<products info="p"></products>
</div>
</div>
I want to start iterating from object having code: 1235 and want to end it after 4 iterations. I am new to AngularJS, any help would be appreciated.

angular.module('myapp', []).controller("products", ["$scope", function($scope) {
$scope.filterData = function(y) {
return function(x) { return x.code >= y;}
}
$scope.products = [{
code: 1234,
icon: "../product/images/abc.jpeg",
name: "One",
description: "lorem ipsum sil vous plait 1",
link: "a.php",
price: 100,
stock: ""
},
{
code: 1235,
icon: "../product/image/def.jpeg",
name: "Two",
description: "lorem ipsum sil vous plait 2",
link: "b.php",
price: 200,
stock: ""
},
{
code: 1236,
icon: "../product/image/def.jpeg",
name: "Three",
description: "lorem ipsum sil vous plait 2",
link: "b.php",
price: 200,
stock: ""
},
{
code: 1237,
icon: "../product/image/def.jpeg",
name: "Four",
description: "lorem ipsum sil vous plait 2",
link: "b.php",
price: 200,
stock: ""
},
{
code: 1238,
icon: "../product/image/def.jpeg",
name: "Five",
description: "lorem ipsum sil vous plait 2",
link: "b.php",
price: 200,
stock: ""
},
{
code: 1240,
icon: "../product/image/def.jpeg",
name: "Six",
description: "lorem ipsum sil vous plait 2",
link: "b.php",
price: 200,
stock: ""
},
{
code: 1241,
icon: "../product/image/def.jpeg",
name: "Seven",
description: "lorem ipsum sil vous plait 2",
link: "b.php",
price: 200,
stock: ""
}
// more such objects
];
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div ng-app="myapp" ng-controller="products">
<div ng-repeat="info in products| filter: filterData(1235) | limitTo:4">
<div class="card">
<div class="card-image">
<img ng-src="{{ info.icon }}">
</div>
<div class="card-content">
<p>{{ info.name }}</p>
<p>{{ info.price }}</p>
</div>
</div>
</div>
</div>
You can have filter function with limitTo pipe.
I hope this will help.

Related

I want show "experience level list" in col - lg - 3 and update data in col - lg - 8

I want show "experience level list" in col - lg - 3 and update data in col - lg - 8.updated output should be display on the col-lg-8 when user select check box from the col-lg-3.I have try the code please help me.
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.2/angular.min.js">
</script>
<script src="https://code.angularjs.org/1.5.0-beta.0/angular-route.min.js"> </script>
</head>
<body>
<div ng-app="demo">
<div class="container">
<div class="col-lg-3">
</div>
<div class="col-lg-9">
<div ng-controller="myCtrl">
<b>Experience Level:</b>
<div ng-repeat="cat in getCategories()">
<b><input type="checkbox" ng-model="filter[cat]" /> {{cat}}</b>
</div>
<hr /> Your Search Results: {{filtered.length}}
<br>
<br>
<div class="row">
<div class="col-lg-12" ng-repeat="w in filtered=(experience | filter:filterByCategory)">
{{w.name}}
<hr />
</div>
</div>
<div>
</div>
</div>
</div>
<script>
var app = angular.module('demo', ['ngRoute']);
app.controller('myCtrl', function($scope, $http) {
$scope.experience = [{
name: "Java developer",
category: "Entry Level ($)"
}, {
name: "Mean-Stack developer",
category: "Entry Level ($)"
}, {
name: "Java developer",
category: "Entry Level ($)"
}, {
name: "web developer",
category: "Entry Level ($)"
}, {
name: "java developer",
category: "Intermediate ($$)"
}, {
name: "Mean-Stack developer",
category: "Intermediate ($$)"
}, {
name: ".net developer",
category: "Intermediate ($$)"
}, {
name: "WCF developer",
category: "Expert ($$$)"
}, {
name: "Spring developer",
category: "Expert ($$$)"
}, {
name: "UI/UX ",
category: "Entry Level ($)"
}
];
$scope.filter = {};
$scope.getCategories = function() {
return ($scope.experience || []).map(function(w) {
return w.category;
}).filter(function(w, idx, arr) {
return arr.indexOf(w) === idx;
});
};
$scope.filterByCategory = function(experience) {
return $scope.filter[experience.category] || noFilter($scope.filter);
};
function noFilter(filterObj) {
for (var key in filterObj) {
if (filterObj[key]) {
return false;
}
}
return true;
}
});
</script>
</body>
</html>
try this code and let me know if it works for you ..
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta.2/angular.min.js">
</script>
<script src="https://code.angularjs.org/1.5.0-beta.0/angular-route.min.js"> </script>
</head>
<body>
<div ng-app="demo">
<div ng-controller="myCtrl">
<div class="container">
<div class="col-lg-3">
<b>Experience Level:</b>
<div ng-repeat="cat in getCategories()">
<b><input type="checkbox" ng-model="filter[cat]" /> {{cat}}</b>
</div>
</div>
<div class="col-lg-8">
<hr /> Your Search Results: {{filtered.length}}
<br>
<br>
<div class="row">
<div class="col-lg-12" ng-repeat="w in filtered=(experience | filter:filterByCategory)">
{{w.name}}
<hr />
</div>
</div>
<div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
<script>
var app = angular.module('demo', ['ngRoute']);
app.controller('myCtrl', function($scope, $http) {
$scope.experience = [{
name: "Java developer",
category: "Entry Level ($)"
}, {
name: "Mean-Stack developer",
category: "Entry Level ($)"
}, {
name: "Java developer",
category: "Entry Level ($)"
}, {
name: "web developer",
category: "Entry Level ($)"
}, {
name: "java developer",
category: "Intermediate ($$)"
}, {
name: "Mean-Stack developer",
category: "Intermediate ($$)"
}, {
name: ".net developer",
category: "Intermediate ($$)"
}, {
name: "WCF developer",
category: "Expert ($$$)"
}, {
name: "Spring developer",
category: "Expert ($$$)"
}, {
name: "UI/UX ",
category: "Entry Level ($)"
}
];
$scope.filter = {};
$scope.getCategories = function() {
return ($scope.experience || []).map(function(w) {
return w.category;
}).filter(function(w, idx, arr) {
return arr.indexOf(w) === idx;
});
};
$scope.filterByCategory = function(experience) {
return $scope.filter[experience.category] || noFilter($scope.filter);
};
function noFilter(filterObj) {
for (var key in filterObj) {
if (filterObj[key]) {
return false;
}
}
return true;
}
});
</script>
</html>

Angularjs ui bootstrap carousel tag not recognized in html editor

I am using angular-bootstrap for the carousel but the issue is, in my html editor <carousel> and <slide> tag is not recognized so the no carousel ,slider, no controls or indicators are displaying :(
i am using angular#1.4.5 , angular-bootstrap#0.13.4 and bootstrap#3.2.0
i tired to change the order also but no results, it works perfectly fine in plunker.
Any suggestions, thanks .
controller-
angular.module('app', ['ui.bootstrap']);
function CarouselDemoCtrl($scope) {
$scope.myInterval = "false";
$scope.slides = [{
Heading: " Cards",
list_categories: [{
id: 'bc1',
name: ' card 1',
response: 'Hello, card 1'
}, {
id: 'bc2',
name: ' card 2',
response: 'Hello, card 2'
}, {
id: 'bc2',
name: ' card 3',
response: 'Hello, card 1'
}]
}, {
Heading: "Documents",
list_categories: [{
id: 'd1',
name: 'Template1',
response: 'Hello, 1'
}, {
id: 'd2',
name: 'Template2',
response: 'Hello, 2'
}, {
id: 'd2',
name: 'Template2',
response: 'Hello, 3'
}]
}, {
Heading: "Other Types",
list_categories: [{
id: 'o1',
name: 'Other1',
response: 'Hello,Others 1'
}, {
id: 'o2',
name: 'Other2',
response: 'Hello, Others 2'
}, {
id: 'o2',
name: 'Other3',
response: 'Hello, Others 3'
}]
}];
}
Html file-
<div ng-app="app">
<div ng-controller="CarouselDemoCtrl" id="slides_control">
<div>
<carousel interval="myInterval">
<slide ng-repeat="slide in slides" active="slide.active">
<h2>{{slide.Heading}}</h2>
<select id="sel" ng-model="selecteditem" class="input-block-level" ng-options="obj.response as obj.name for obj in slide.list_categories" >
<option value="">Select template</option>
</select>
<h4>Response: {{selecteditem}}</h>
</slide>
</carousel>
</div>
</div>
</div>
here is order i am using-
The name of the tag has changed to uib-carousel and uib-slide
Exempli gratia:
<uib-carousel interval="myInterval">
<uib-slide ng-repeat="slide in slides track by $index" active="slide.active">
<img ng-src="{{slide.image}}" style="margin:auto;">
</uib-slide>
</uib-carousel>

AngularJS tabs acting weird

I was wondering if some of you could lighten me and try to explain what I miss in this : http://plnkr.co/edit/opxB2Jfi0Xf0Tq1780vz?p=preview
Looks quite simple to me but does not work.
My code:
<section ng-app="myApp">
<div ng-controller="myCtrl">
<ul ng-init="tab=1">
<li ng-repeat="item in data">
<a href ng-click="tab = item.thingy">{{item.name}}</a>
</li>
</ul>
<div ng-repeat="item in data" ng-show="tab === item.thingy">
<img ng-src="{{item.img}}" width="50px"><br>
{{item.year}}</div>
</div>
</section>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', ['$scope',
function($scope) {
$scope.data = [{
name: "First",
title: "oneTitle",
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
year: "2013",
img: "http://static.hdw.eweb4.com/media/wp_400/1/5/42735.jpg",
thingy: 1
}, {
name: "third",
title: "twoTitle",
description: "Quisque pulvinar libero sed eros ornare",
year: "2014",
img: "http://static.hdw.eweb4.com/media/wp_400/1/1/8519.jpg",
thingy: 2
}, {
name: "Second",
title: "threeTitle",
description: "Cras accumsan massa vitae tortor vehicula .",
year: "2015",
img: "http://static.hdw.eweb4.com/media/wp_400/1/5/43326.jpg",
thingy: 3
}, {
name: "fourth",
title: "FourTitle",
description: "Suspendisse vitae mattis magna.",
year: "2011",
img: "http://static.hdw.eweb4.com/media/wp_400/1/5/42413.jpg",
thingy: 4
}];
}
]);
</script>
Thank you in advance!
modifications in script.js:
var app = angular.module('myApp', []);
app.controller('myCtrl', ['$scope', function ($scope) {
$scope.data = [{
name: "First",
title: "oneTitle",
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
year: "2013",
img: "http://static.hdw.eweb4.com/media/wp_400/1/5/42735.jpg",
thingy: 1
}, {
name: "third",
title: "twoTitle",
description: "Quisque pulvinar libero sed eros ornare",
year: "2014",
img: "http://static.hdw.eweb4.com/media/wp_400/1/1/8519.jpg",
thingy: 2
}, {
name: "Second",
title: "threeTitle",
description: "Cras accumsan massa vitae tortor vehicula .",
year: "2015",
img: "http://static.hdw.eweb4.com/media/wp_400/1/5/43326.jpg",
thingy: 3
}, {
name: "fourth",
title: "FourTitle",
description: "Suspendisse vitae mattis magna.",
year: "2011",
img: "http://static.hdw.eweb4.com/media/wp_400/1/5/42413.jpg",
thingy: 4
}];
$scope.details = $scope.data[0];
$scope.GetDetails = function(obj)
{
$scope.details = obj;
}
}]
);
In HTML:
<section ng-app="myApp">
<div ng-controller="myCtrl">
<ul ng-init="tab=1">
<li ng-repeat="item in data">
<a href ng-click="GetDetails(item);">{{item.name}}</a>
</li>
</ul>
<div>
{{details.thingy}} <br/>
{{details.name}}<br/>
{{details.title}}<br/>
{{details.description}}<br/>
{{details.year}}<br/>
<img ng-src="{{details.img}}" width="50px"><br>
</div>
</div>
</section>
Basically second ng-repeat is not required

How to binding select with radio buttons in ng-repeat? AngularJS

How to binding select with radio buttons in ng-repeat?
I wona get tax from selected radio button in inner select.
When user picks town and price, radio button must be changed accordingly.
Help!
jsfiddle.net/hanze/pqh4eq96
html
<h1>Select </h1>
<div ng-app="" ng-controller="OrderCtrl">
<div class="radio" ng-repeat="delivery in deliveries">
<label>
<input type="radio" name="radioDelivery" ng-value="delivery" ng- model="$parent.selectedDelivery">
{{delivery.name}}. {{delivery.desc}}
<select>
<option ng-repeat="tax in delivery.tax" ng-value="tax" ng- model="$parent.selectedTax">{{tax.town}} {{tax.price}} $ </option>
</select>
</label>
</div>
<br>Delivery: {{selectedDelivery.name}}
<br>Tax delivery: {{ }} /// ??</div>
js
OrderCtrl = function ($scope) {
$scope.deliveries = [{
name: "RussianPOST",
tax: [{
town: "Moscow",
price: 10,
}, {
town: "Izhevsk",
price: 30,
}]
}, {
name: "DHL",
tax: [{
town: "Moscow",
price: 50,
}, {
town: "Izhevsk",
price: 100,
}]
}
];
$scope.selectedDelivery = $scope.deliveries[0];
}
In your script:
OrderCtrl = function ($scope) {
$scope.deliveries = [{
name: "RussianPOST",
tax: [{
town: "Moscow",
price: 10,
}, {
town: "Izhevsk",
price: 30,
}]
}, {
name: "DHL",
tax: [{
town: "Moscow",
price: 50,
}, {
town: "Izhevsk",
price: 100,
}]
}
];
$scope.selectedDelivery = $scope.deliveries[0];
$scope.TaxSelect=function(tax){
$scope.selectedTax = tax;
}
}
Your option (I am not sure if ng-model is needed here, but i am letting it stay):
<option ng-repeat="tax in delivery.tax" ng-value="tax" ng-model="$parent.selectedTax" ng-click="TaxSelect(tax)">{{tax.town}} {{tax.price}} $</option>
Your display:
Tax delivery: {{ selectedTax.town}}, {{ selectedTax.price}}
Fiddle DEMO

How to get data from radio buttons? angularjs

i wona get price and name of chosen radio buttons. its easy with simple html tags.
But I stacked when i trying generate radio buttons via angularjs from array (items)
Help!
http://jsfiddle.net/hanze/j9x23apu/
html
<h1>Select </h1>
<div ng-app="" ng-controller="OrderCtrl">
<div ng-repeat="item in items">
<div class="radio">
<label>
<input type="radio" name="item" ng-model="item" ng-checked="{{item.checked}}">
{{item.name}} +{{item.price}} $.</label>
</div>
</div>
Your choice: {{}} **what i must write here?**
<br>
Price: {{}} **and here?**
</div>
js
OrderCtrl = function ($scope) {
$scope.items = [{
name: 'None',
value: "no",
price: 0,
checked: true
}, {
name: 'Black',
value: "black",
price: 99,
checked: false
}, {
name: 'White',
value: "white",
price: 99,
checked: false
}, {
name: 'Barhat',
value: "barhat",
price: 49,
checked: false
}, {
name: 'Barhat',
value: "cream",
price: 49,
checked: false
}]
You can look at the angularjs documentaion about radio buttons here. You don't need to use ng-checked here. Use ng-value to set the value when redio is selected.
I changed your jsfiddle post.
Your are missing the closing tag for your input. And when you have ng-repeat you have a seperate scope. In this case you need $parent.selectedItem to hold your selected elements.
I have updated the JSFiddle with a working state solution.
<div ng-app="" ng-controller="OrderCtrl">
<div ng-repeat="item in items" style="text-indent: 30px">
<input type="radio" name="itemRadio" ng-model="$parent.selectedItem" ng-value="item.name"/>
{{item.name + '-' + item.price}}$.
</div>
Your choice: {{selectedItem}}
</div>
jsFiddle: http://jsfiddle.net/j9x23apu/16/
html
<div ng-app="" ng-controller="OrderCtrl">
<div ng-repeat="item in items" style="text-indent: 30px">
<label>
<input type="radio" name="item" ng-checked="{{item.checked}}" ng-click="change_item($event)" item-name="{{item.name}}" item-price="{{item.price}}" /> {{item.name}} + {{item.price}}
</label>
</div>
Your choice: {{selectedName}}
<br />
Price: {{selectedPrice}}
</div>
js
OrderCtrl = function ($scope) {
$scope.change_item = function(e) {
$scope.selectedName = e.target.attributes['item-name'].value;
$scope.selectedPrice = e.target.attributes['item-price'].value;
};
$scope.items = [{
name: 'None',
value: "no",
price: 0,
checked: true
}, {
name: 'Black',
value: "black",
price: 99,
checked: false
}, {
name: 'White',
value: "white",
price: 99,
checked: false
}, {
name: 'Barhat',
value: "barhat",
price: 49,
checked: false
}, {
name: 'Barhat',
value: "cream",
price: 49,
checked: false
}]
}

Resources