How to set dynamic limit in ng-repeat angular js - arrays

How to set dynamic limit in ng-repeat ? I want to set limit if window width is less than 750 px than set limit to 1.
Here is my controller function:
$scope.$watch('window.innerWidth', function() {
$scope.wsizewindow = window.innerWidth;
if($scope.wsizewindow < 750) {
$scope.limit = 0;
} else {
}
console.log($scope.wsizewindow);
});
Here is my html:
<flex-slider slider-id=""
flex-slide="responsibilities in roles.responsibilities track by $index | limitTo: limit" animation="slide"
animation-loop="false" item-width="350" item-margin="1" keyboard="false"
as-nav-for="#slider" slideshow="false" control-nav="false">
<li class="col-sm-3" ng-if="responsibilities.responsibilityName!=''">
<div class="new-box" ng-if="$index!=roles.responsibilities.length-1">
<div class="panel-heading1">Responsibility</div>
<div class="col-xs-12 col-sm-12">
<div class="strike" >
<span >{{responsibilities.responsibilityName}}</span>
</div>
</div>
<div class="col-xs-12 col-sm-12 box">
<div class="form-group list" mb-scrollbar="scrollbar('vertical', true)">
<div class="panel panel-default" style="cursor: pointer;" ng-click="invokeReviewProcess(departments,roles,responsibilities,processes,isCompAdmin);" ng-repeat="processes in responsibilities.processes">
<span class="id">{{processes.name}}</span>
</div>
</div>
<div align="center" id="addProcessLink" ng-if="isAdmin === true && isGraph">
<a class="primary-link" ng-click="addProcess(departments.id,roles.id,responsibilities.id)" href="#"> Add a Process</a>
</div>
</div>
</div>
</li>
</flex-slider>
I want to set limit here:
responsibilities in roles.responsibilities track by $index | limitTo: limit.
Give me some suggestions. I am new to AngularJs. thanks.

Use a function defined in your controller:
responsibilities in roles.responsibilities track by $index | limitTo: (setLimit())
In your controller:
$scope.setLimit = function() {
//check if window width less than 750, if yes, return 1 else some other limit
}

Related

how can fix data not showing with angularjs and laravel

Is anyone help me, when I start to code to show data in blade with angularJS, data not showing in the page, even the data exists in the console. This following code in my app.js
// app.js
$scope.view_tab = 'shop1';
$scope.changeTab = function(tab) {
$scope.view_tab = tab;
}
// List product
$scope.loadProduct = function () {
$http.get('/getproduct').then(function success(e) {
console.log(e.data);
$scope.products = e.data.product;
$scope.totalProduct = $scope.products.length;
$scope.currentPage = 1;
$scope.pageSize = 9;
$scope.sortKey = 'id_kain';
};
//index.blade.php
<div class="shop-top-bar">
<div class="shop-tab nav">
<a ng-class="{'active': view_tab == 'shop1'}" ng-click="changeTab('shop1')" data-toggle="tab">
<i class="fa fa-table"></i>
</a>
<a ng-class="{'active': view_tab == 'shop2'}" ng-click="changeTab('shop2')" data-toggle="tab">
<i class="fa fa-list-ul"></i>
</a>
</div>
</div>
<div class="shop-bottom-area mt-35">
<div class="tab-content jump">
<div class="tab-pane" ng-class="{active: view_tab == 'shop1'}">
<div class="row">
<div ng-repeat="data in filtered = ( products | filter:search ) | orderBy:sortData | itemsPerPage: 9" class="col-xl-4 col-md-6 col-lg-6 col-sm-6">
<div class="product-wrap mb-25 scroll-zoom">
<div class="product-img">
<a ng-click="showForm(data)">
<img class="default-img" ng-src="#{{ data.gambar_kain[0].gambar_kain }}" alt="">
<img class="hover-img" ng-src="#{{ data.gambar_kain[1].gambar_kain }}" alt="">
</a>
</div>
<div class="product-content text-center">
<h3>#{{data.nama_kain}}</h3>
<div class="product-price">
<span>#{{numberingFormat(data.data_kain[0].harga_kain)}}</span>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="tab-pane" ng-class="{active: view_tab == 'shop2'}">
<div dir-paginate="datas in filtered = ( products | filter:search ) | orderBy:sortData | itemsPerPage:9" class="shop-list-wrap mb-30">
<div class="row">
<div class="col-xl-4 col-lg-5 col-md-5 col-sm-6">
<div class="product-wrap">
<div class="product-img">
<a ng-click="showForm(datas)">
<img class="default-img" ng-src="#{{ datas.gambar_kain[0].gambar_kain }}" alt="">
<img class="hover-img" ng-src="#{{ datas.gambar_kain[1].gambar_kain }}" alt="">
</a>
</div>
</div>
</div>
<div class="col-xl-8 col-lg-7 col-md-7 col-sm-6">
<div class="shop-list-content">
<h3>#{{data.nama_kain}}</h3>
<div class="product-list-price">
<span>#{{numberingFormat(datas.data_kain[0].harga_kain)}}</span>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="pro-pagination-style text-center mt-30">
<dir-pagination-controls
max-size="1"
direction-links="true"
boundary-links="true" >
</dir-pagination-controls>
</div>
</div>
Data still not shows in the page, there are not errors in this page, but I don't know how can I fix this.
As I know there is two way to show data in Angular JS when you define the scopes then you need the ng-bind or ng-model to show data in the front end.
1- AngularJS Data Binding
live Example https://www.w3schools.com/code/tryit.asp?filename=G2DQQ2GSJ3EO
<div ng-app="myApp" ng-controller="myCtrl">
<p ng-bind="firstname "></p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstname = "John will be change";
});
</script>
2-AngularJS ng-model Directive
Live Example https://www.w3schools.com/code/tryit.asp?filename=G2DQQEUEPSOC
<div ng-app="myApp" ng-controller="myCtrl">
Name: <input ng-model="name">
<h1>You entered: {{name}}</h1>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.name = "John Doe";
});
</script>
NOTE: try with static data and then check that you have valid data to make it dynamic
I hope this helps you!

How I can share the value between two pages in ionic v-1

I am trying to add the value of both pages on selection.
It is adding on individual pages. But while I am moving to the next page the Total value comes Zero. I am trying to add pass total added value from one page to another page.
As I search , I found that there is a need to create a service controller to call a global scope value.
<!-- page one view page -->
<ion-content class="search-main padding">
<div class="list card">
<div class="item item-body no-padding stable">
<div class="row no-padding border-bottom" ng-controller="Ctrl3">
<div class="col padding border-right" ng-click="openDateCheckIn()">
<div class="margin-bottom-10" style="color:#0c60ee;"><i class="icon icon-money"></i> Total Value =></div>
</div>
<div class="col padding"><h2 style="text-align:center; margin-top: 3px;">Rs. <b>{{rootSum()}}</b></h2></div>
</div>ass="col padding"><h2 style="text-align:center; margin-top: 3px;">Rs. {{rootSum()}}</h2></div>
</div>
<div ng-controller="Ctrl1">
<div ng-repeat="gender in genders">
<div class="row row-no-padding no-margin padding-important" style="color: #000;">
<div class="col col-70"><i class="ion ion-ios-circle-filled"></i> <span> {{gender.id}}</span></div>
<div class="col col-20"><span>Rs {{gender.value}}</span></div>
<div class="col col-10"><div><label class="search-checkbox item item-checkbox" style="margin: -5px 0px 0px -2px;"><div class="checkbox checkbox-input-hidden disable-pointer-events checkbox-circle"><input id="{{gender}}" type="checkbox" value="{{gender.value}}" ng-checked="selection.indexOf(gender) > -1" ng-click="toggleSelection(gender)" /><i class="checkbox-icon"></i></div><div class="item-content disable-pointer-events" ng-transclude=""></div></label></div>
</div></div>
</div></div>
</ion-content>
<!-- Page two view page -->
<ion-content class="search-main padding">
<div class="list card">
<div class="item item-body no-padding stable">
<div class="row no-padding border-bottom" ng-controller="Ctrl3">
<div class="col padding border-right" ng-click="openDateCheckIn()">
<div class="margin-bottom-10" style="color:#0c60ee;"><i class="icon icon-money"></i> Total Value =></div>
</div>
<div class="col padding"><h2 style="text-align:center; margin-top: 3px;">Rs. <b>{{rootSum()}}</b></h2></div>
</div>
<div ng-controller="Ctrl2">
<div ng-repeat="gender in genders">
<div class="row row-no-padding no-margin padding-important" style="color: #000;">
<div class="col col-70"><i class="ion ion-ios-circle-filled"></i> <span> {{gender.id}}</span></div>
<div class="col col-20"><span>Rs {{gender.value}}</span></div>
<div class="col col-10"><div><label class="search-checkbox item item-checkbox" style="margin: -5px 0px 0px -2px;"><div class="checkbox checkbox-input-hidden disable-pointer-events checkbox-circle"><input id="{{gender}}" type="checkbox" value="{{gender.value}}" ng-checked="selection.indexOf(gender) > -1" ng-click="toggleSelection(gender)" /><i class="checkbox-icon"></i></div><div class="item-content disable-pointer-events" ng-transclude=""></div></label></div>
</div></div>
</div></div>
</div>
</div>
</ion-content>
Controller page --
//Ladies Makeup Party
.controller('Ctrl1', function($scope ,$rootScope) {
$scope.genders=[{
'id':'Krylon Party Makeup', 'value':2000 },
{'id':'Makeup Studio', 'value':2200 },
{'id':'Party Makeup(MAC)', 'value':2500 },
{'id':'Party Mackeup(Airbrush)', 'value':3500 }
];
$rootScope.selection=[];
$scope.toggleSelection = function toggleSelection(gender) {
var idx = $scope.selection.indexOf(gender);
if (idx > -1) {
$scope.selection.splice(idx, 1);
}
else {
$scope.selection.push(gender);
}
};
})
//Ladies Makeup Pre-bridal
.controller('Ctrl2', function($scope ,$rootScope) {
$scope.genders=[{
'id':'Silver', 'value':10000 },
{'id':'Gold', 'value':12000 },
{'id':'Platinum', 'value':16000 }
];
$rootScope.selection=[];
$scope.toggleSelection = function toggleSelection(gender) {
var idx = $scope.selection.indexOf(gender);
if (idx > -1) {
$scope.selection.splice(idx, 1);
}
else {
$scope.selection.push(gender);
}
};
})
// Add total value on select
.controller('Ctrl3', function($scope ,$rootScope) {
$rootScope.rootSum=function(){
var total=0;
for(var i = 0; i < $rootScope.selection.length; i++){
var product = $rootScope.selection[i];
total += (product.value);
}
return total
};
}) *
There are two problems I can see here.
You are initialising $rootScope.selection = [] at every controller, this deletes all previous changes. Initialise it at start of page load inside run block.Use this link to implement run run.
myApp.run(function() {
console.log("app run");
});
You are not transferring selected data to $rootScope.selection variable. You need to append your selected product in rootScope and make sure no item is repeated. Instead of array, I recommend you to use object with 'ids' as attrinute name, this will ensure that no item is repeated inherently.

check if $index in ng-repeat is null

I have a grid view with two grids per row. This is the code:
<ion-content scroll="true" class="has-header">
<div class="row" style="padding-bottom: 40px;">
</div>
<div ng-repeat="i in products">
<div class="row" ng-if="$even">
<div class="col col-50">
<a class="item item-thumbnail-center" href="#">
<img src="http://{{host}}/{{
products[$index].prod_img
}}" class="img-thumbnail">
<h2>{{products[$index].prod_name}}</h2>
<p>Php {{products[$index].prod_price}}.00</p>
</a>
</div>
<div class="col col-50" ng-if="$index + 1">
<a class="item item-thumbnail-center" href="#">
<img src="http://{{host}}/{{
products[$index + 1].prod_img
}}" class="img-thumbnail">
<h2>{{products[$index + 1].prod_name}}</h2>
<p>Php {{products[$index + 1].prod_price}}.00</p>
</a>
</div>
</div>
</div>
</ion-content>
controller.js
$http.get("http://"+ $scope.host +"/mobile/get_products.php?cat_id=" + $stateParams.cat_id)
.then(function(response) {
$scope.products = response.data;
});
The problem is when the size of the array products is an odd, the second grid in the last row still shows up and certainly no item is displayed in there because the item is specified by the index is null. I've found some other solution on how to make this by configuring the array right from the controller but I want to ask if there is any way to check using an angular directive whether $index is null, and if true, the grid will be hidden.
you can use ng-class directive and put a css class which will get true when products[$index] === null | products[$index+1] === null. it will be like
ng-class={'hide-row': products[$index] === null | products[$index+1] === null}
where css
.hide-row{ display : none;}

set dragable false for particular div in angular js with ionic

This is used for match the following option.
While I drag the div id 'div2' along with this first div id 'div1' also to be dragged. how to set draggable false for div id 'div1'
.controller('BEGINNER_UNIT_1_CONVERSATION_ACTIVITY_2', function($scope, $stateParams, $http)
{
$scope.OnDropComplete = function (index, source)
{
var otherObj = $scope.category_Question[index];
var otherIndex = $scope.category_Question.indexOf(source);
$scope.category_Question[index] = source;
$scope.category_Question[otherIndex] = otherObj;
}
})
--------------------------------------------------------
<div ng-repeat="source in category_Question" ng-controller="BEGINNER_UNIT_1_CONVERSATION_ACTIVITY_2" >
<div class="card" id="div1" draggable="false">
<div class="item item-divider">{{ $index+1 }}.Sentence </div>
<div class="item item-text-wrap" style="color:#CC0066;font-weight:bold;">{{ source.Sentance }}</div>
</div>
<div class="card" id="div2" ng-drop="true" ng-drop-success="OnDropComplete($index,$data)">
<div ng-drag="true" ng-drag-data="source" ng-class="source.Response">
<div class="item item-divider">Response</div>
<div class="item item-text-wrap"> {{ source.Response }} </div>
</div>
</div>
</div>
--------------------------------------------------------
You're using the wrong attribute. draggable="false".
It should be ng-cancel-drag
Your html would look like:
<div class="card" id="div1" ng-cancel-drag>
...
</div>

AngularJS - How to generate random value for each ng-repeat iteration

I am trying to create random span sized divs(.childBox) of twitter bootstrap using AngularJS.
<div ng-controller="HomeCtrl">
<div class="motherBox" ng-repeat="n in news">
<div class="childBox" class="col-md-{{boxSpan}} box">
<a href="#" class="thumbnail">
<img src="{{holderLink}}" height="200px" alt="100x100">
<p class="tBlock"> {{n.title}} </p>
</a>
</div>
</div>
</div>
controller('HomeCtrl', ['$scope', '$http', function($scope,$http) {
$http.get('news/abc.json').success(function(data) {
$scope.news = data;
});
$scope.holderSize = 150;
$scope.holderLink = 'http://placehold.it/'+$scope.holderSize+'x'+$scope.holderSize;
$scope.boxSpan = getRandomSpan();
function getRandomSpan(){
return Math.floor((Math.random()*6)+1);
};
}])
I want to create different integer value for boxSpan for each .childBox div but all .childBox have same boxSpan value. Although everytime i refresh page boxSpan creates random value.
How can i generate different/random value for each ng-repeat iteration?
Just call add getRandomSpan() function to your scope and call it in your template:
$scope.getRandomSpan = function(){
return Math.floor((Math.random()*6)+1);
}
<div ng-controller="HomeCtrl">
<div class="motherBox" ng-repeat="n in news">
<div class="childBox" class="col-md-{{getRandomSpan()}} box">
<a href="#" class="thumbnail">
<img src="{{holderLink}}" height="200px" alt="100x100">
<p class="tBlock"> {{n.title}} </p>
</a>
</div>
</div>
</div>
As an alternative to the accepted answer, since you're likely to reuse this function, you can turn it into a filter for convenience:
angular.module('myApp').filter('randomize', function() {
return function(input, scope) {
if (input!=null && input!=undefined && input > 1) {
return Math.floor((Math.random()*input)+1);
}
}
});
Then, you can define the max and use it anywhere on your app:
<div ng-controller="HomeCtrl">
<div class="motherBox" ng-repeat="n in news">
<div class="childBox" class="col-md-{{6 | randomize}} box">
<a href="#" class="thumbnail">
<img src="{{holderLink}}" height="200px" alt="100x100">
<p class="tBlock"> {{n.title}} </p>
</a>
</div>
</div>
</div>
Improvisation of the accepted answer to prevent digest overflow:
var rand = 1;
$scope.initRand = function(){
rand = Math.floor((Math.random()*6)+1)
}
$scope.getRandomSpan = function(){
return rand;
}
<div ng-controller="HomeCtrl">
<div class="motherBox" ng-repeat="n in news">
<div class="childBox" ng-init="initRand()" class="col-md-{{getRandomSpan()}} box">
<a href="#" class="thumbnail">
<img src="{{holderLink}}" height="200px" alt="100x100">
<p class="tBlock"> {{n.title}} </p>
</a>
</div>
</div>
</div>

Resources