Infinite loop Error: [$rootScope:infdig] - angularjs

I created a function delay(), to get random delay number for animation, everything is working fine, but in console I get this infinite loop Error: [$rootScope:infdig]. I'd like to set delay() iteration number = work record number, how can I do it ?
HTML:
<div id="work" class="work" ng-controller="WorkCtrl">
<ul class="grid">
<li class="wow zoomIn" data-wow-delay="0.{{ delay() }}s" ng-repeat="w in work | orderBy:'id':true">
<a href="{{ w.link }}" target="blank_">
<div class="background"></div>
<h3 class="name">{{ w.name }}</h3>
<p class="description">{{ w.description }}</p>
<img ng-src="{{ w.image_path }}">
</a>
</li>
</ul>
JS:
var app = angular.module("portfolio", []);
app.controller('WorkCtrl', function($scope, $http) {
$http.get('work.json').success(function(work) {
$scope.work = work;
});
$scope.delay = function(minNum, maxNum) {
minNum = 0;
maxNum = 5;
return (Math.floor(Math.random()*(maxNum - minNum + 1)) + minNum);
};
});

Thank You Blazemonger, I wrote a new array with an extra attribute delay, now it works like a charm with no errors.
HTML:
<div id="work" class="work" ng-controller="WorkCtrl">
<ul class="grid">
<li class="wow zoomIn" data-wow-delay="0.{{ w.delay() }}s" ng-repeat="w in workList | orderBy:'item.id':true">
<a href="{{ w.item.link }}" target="blank_">
<div class="background"></div>
<h3 class="name">{{ w.item.name }}</h3>
<p class="description">{{ w.item.description }}</p>
<img ng-src="{{ w.item.image_path }}">
</a>
</li>
</ul>
</div>
JS:
$scope.workList = [];
angular.forEach($scope.work, function(item) {
minNum = 0;
maxNum = 5;
$scope.workList.push({
item: item,
delay: (Math.floor(Math.random()*(maxNum - minNum + 1)) + minNum)
});
});

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 to set dynamic limit in ng-repeat angular js

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
}

Bookmarking article in ionic app

How to bookmark article in ionic app
<div class="container" on-tap="toggleHeader()" >
<!-- on-tap="toggleHeader()" -->
<div id="test" class="slideItem" ng-class="{'list list-inset-hide': Aindex > $index + 1, 'back-items': Aindex < $index+1}" ng-repeat="Listnews in AllnewsList" ng-style="{'height':'{{hgt}}','z-index': (AllnewsList.length - $index)}" on-swipe-up="swipeUp($event)" on-swipe-down="swipeDown($event)">
<div ng-class="{'satin': Aindex < $index+1, 'not-satin': Aindex != $index}" >
</div>
<ul class="list-unstyled cs-tag-item-grp newsDiv">
<li class="clearfix">
<div class="newsCnt">
<div ng-show="Listnews.ImageUrl !=' ' " class="newsImageContainer">
<img class="newsImage" ng-src="{{Listnews.ImageUrl}}"/>
</div>
<div class="newTitle pLR5">{{Listnews.Heading}}</div>
<div class="newsDisc pLR5">{{Listnews.Para | limitTo: 300}} {{Listnews.Para.length > 300 ? '…':''}}</div>
<div class="newsEdit pLR5"><strong>Starticle</strong> by - {{Listnews.Editor}} / {{Listnews.PublishedDate | date:'dd MMM'}} </div>
<div class="newSrc pLR5" ng-show="Listnews.SourceLink" >more at - <a ng-href="{{Listnews.SourceLink}}" onclick="window.cordova.InAppBrowser.open(this.href, '_blank', 'location=yes'); return false;">{{Listnews.SourceName}}</a>
</div>
<div class="tabs tabs-icon-only" >
<a class="tab-item" >
<a class="button icon ion-share" ng-click="shareAnywhere(Listnews.ImageUrl,Listnews.Heading,Listnews.Para,Listnews.SourceLink)"></a>
</a>
<a class="tab-item">
<a ng-show="!bookmarkstate" class="button icon ion-ios-bookmarks-outline" ng-click="bookmark(true)"></a>
<a ng-show="bookmarkstate" class="button icon ion-ios-bookmarks" ng-click="bookmark(false)"></a>
</a>
<a class="tab-item">
<a class="button icon ion-chatbox-working" ng-click="commentModalopen()"></a>
</a>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
bookmark function
$scope.bookmark = function(state)
{
$scope.info = null;
if(!localStorage.getItem("localnews")){
var book_data = {data: []};
var notID = 0 ;
window.localStorage.setItem("book_data", JSON.stringify(book_data));
window.localStorage.setItem("id", JSON.stringify(notID));
}
$scope.info = JSON.parse(window.localStorage.getItem("book_data"));
$scope.bookmarkstate = state
var page = document.getElementById("localnews").value;
if(page == null)
{
alert("Bookmark Not added");
return;
}
var id = JSON.parse(window.localStorage.getItem("book_data"));
var array = {id:id};
$scope.info.data[$scope.info.data.length] = array;
window.localStorage.setItem("rp_data", JSON.stringify($scope.info));
window.localStorage.setItem("id", JSON.stringify(id + 1));
alert("Bookmark Added")
document.getElementById("Aindex").value =" ";
$scope.getBookmarked();
}

how i get total of item in ng-repeat

I'm trying to get the overall number of items in each iteration.my code is this
<ul class="phones">
<li ng-repeat="phone in phones | filter:query | orderBy:orderProp"
ng-init="index()" class="thumbnail">
<a href="#/phones/{{phone.id}}" class="thumb">
<img ng-src="{{phone.imageUrl}}">
</a>
{{phone.name}}
<p>{{phone.snippet}}</p>
</li>
</ul>
<div>
{{ total }}
</div>
Controller
phonecatApp.controller('PhoneListCtrl', function($scope, $http)
{
$scope.total =0;
$scope.index =function()
{
$scope.total ++;
}
}
By dynamic you mean total no of phones currently displayed after filter is applied.
<ul class="phones">
<li ng-repeat="phone in filtered = (phones | filter:query | orderBy:orderProp)"
ng-init="index()" class="thumbnail">
<a href="#/phones/{{phone.id}}" class="thumb">
<img ng-src="{{phone.imageUrl}}">
</a>
{{phone.name}}
<p>{{phone.snippet}}</p>
</li>
</ul>
<div>
{{ filtered.length }}
</div>
To make more sense of data (Bonus Tip :) )
<div ng-show="query">
<ng-pluralize count="filtered.length" when="{'0': ' No search result ', 'one': ' 1 search result ', 'other': ' {} search results '}"></ng-pluralize> for {{query}}
</div>
Are you trying to get total number of phones?
you can simply use length property in expression instead {{ total }}
{{ phones.length }}

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