ng-style assign dynamic scope variable from $index - angularjs

I'm trying to assign a scope variable's value to an ng-style directive but the name is not parsing.
What am I doing wrong? When you press the TEST button it should turn the text to RED but the scope variable is not parsing as it's name is dynamic. I've tried multiple different syntax which all didn't work.
Here is the html:
<div ng-app="myApp" ng-controller="MyCtrl">
<button ng-click="Test()">test</button>
<div ng-repeat="item in items">
<div ng-style="{{'DynamicVariable-'+$index}}">
{{someone.name}}
</div>
</div>
</div>
controller code
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', ['$scope', function($scope) {
$scope.someone = {
name: 'John'
};
$scope.items=[1,2,3,4,5,6,7,8,9];
$scope.mainId = 1;
$scope.Test=function(){
for(var i=0;i<$scope.items.length;i++){
$scope["DynamicVariable-"+i]={color:'red'};
}
console.log($scope);
};
}]);
js fiddle that doesn't work:
http://jsfiddle.net/avboivin/0qov365b/4/

It has a problem with '-', try to remove it
html:
<div ng-app="myApp" ng-controller="MyCtrl">
<button ng-click="Test()">test</button>
<div ng-repeat="item in items">
<div ng-style="{{'DynamicVariable'+$index}}">
{{ someone.name }}
</div>
</div>
</div>
controller:
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', ['$scope', function ($scope) {
$scope.someone = {
name: 'John'
};
$scope.items = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$scope.mainId = 1;
$scope.Test = function () {
for (var i = 0; i < $scope.items.length; i++) {
$scope["DynamicVariable" + i] = { color: 'red' };
}
console.log($scope);
};
}]);
fork

Related

I need to print array values in angular js using ng-repeat?

I'm having an array values like [0,1,5,10].
I need the output to be like this
0
1
b
b
b
5
b
b
b
b
10
For the empty values inside the array, I want to print as b. How can i do it with the ng-repeat and I dont want the static coding it want it to be dynamic.
my code is like this :
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$scope.q = [0,1,5,10];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="customersCtrl">
<div ng-repeat="i in q">{{i}}</div>
</div>
And one more thing id don't want to create a new array using array.push() method.
Please help me on this, thanks in advance.
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$scope.q = [0,1,5,10];
$scope.temp = function(){
var answer = [];
for(var i = $scope.q[0]; i <= $scope.q[$scope.q.length-1]; i++)
answer.push($scope.q.indexOf(i) == -1 ? 'b' : i);
return answer;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="customersCtrl">
<div ng-repeat="i in temp() track by $index">{{i}}</div>
</div>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$scope.q = [0, 1, 5, 10];
$scope.getNumber = function(num) {
if (num > 0) return new Array(num);
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="customersCtrl">
<div ng-repeat="i in q">
<div ng-repeat="j in getNumber(q[$index] - q[$index-1]-1) track by $index">b</div>
{{i}}
</div>
</div>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$scope.q = [0,1,5,10];
$scope.empties = function(index) {
return index === 0 ? [] : new Array($scope.q[index] - $scope.q[index - 1] - 1);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="customersCtrl">
<div ng-repeat = "i in q">
<div ng-repeat = "empty in empties($index) track by $index">b</div>
<div>{{i}}</div>
</div>
</div>
</div>

Angular JS orderBy dynamic values

I have an object something like
x=[{a:1,b:2,c:3,d:'A'},{a:3,b:3,c:1,d:'C'},{a:2,b:1,c:2,d:'B'}]
View In HTML there are 3 buttons
<button ng-click="sortbyA()">
<button ng-click="sortbyB()">
<button ng-click="sortbyC()">
And a pop-up
<div ng-repeat='i in x | orderBy:sortBy'>
<table>
<tr><td>{{i.d}}</td></tr>
</table>
</div>
Angular JS Controller:
var app = angular.module("myApp", []);
app.controller("myCtrl", ['$compile', '$http', '$scope', function ($compile, $http, $scope) {
$scope.get_results=function(){
var response= $http.get();
response.success(function(){
$scope.sortBy='a';
});
}
$scope.sortbyA=function(){
$scope.sortBy='a';
}
$scope.sortbyB=function(){
$scope.sortBy='b';
}
$scope.sortbyC=function(){
$scope.sortBy='c';
}
}])
Use case is to populate data of table based different clicks(A,B,C) with their corresponding property in x
Eg: If click on B,table should have data sorted by attribute :b,
If click on C,table should have data sorted by attribute :c
In HTML
var app = angular.module('app', []);
app.controller('MainCtrl', function($scope) {
$scope.data = [{ a: 1, b: 2, c: 3, d: 'A' }, { a: 3, b: 3, c: 1, d: 'C' }, { a: 2, b: 1, c: 2, d: 'B' }];
$scope.sortBy = 'a'; //for default sorting
$scope.sortItems = function(key) {
$scope.sortBy = key; // key on which prop you want to sort
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app">
<div class="" ng-controller="MainCtrl">
<button ng-click="sortItems('a')">a</button>
<button ng-click="sortItems('b')">b</button>
<button ng-click="sortItems('c')">c</button>
<button ng-click="sortItems('d')">d</button>
<br />
<div ng-repeat='i in data | orderBy:sortBy'>
<table>
<tr>
<td>{{i.a}}</td>
<td>{{i.b}}</td>
<td>{{i.c}}</td>
<td>{{i.d}}</td>
</tr>
</table>
</div>
</div>
</body>
Response data:
x=[{a:1,b:2,c:3,d:'A'},{a:3,b:3,c:1,d:'C'},{a:2,b:1,c:2,d:'B'}]
Your buttons:
<button ng-click="sortBy = 'a'">
<button ng-click="sortBy = 'b'">
<button ng-click="sortBy = 'c'">
Your table:
<div ng-repeat='i in x | orderBy:sortBy'>
<!-- blabla -->
Your js:
var app = angular.module("myApp", []);
app.controller("myCtrl", ['$compile', '$http', '$scope', function ($compile, $http, $scope) {
$scope.sortBy = 'a';
$scope.get_results=function(){
var response= $http.get();
response.success(function(){
//blabla
});
}
}])

infinite-scroll method not triggering

I'm using ngInfiniteScroll in my project to load large data into the UI.
Here is my HTML,
<div infinite-scroll='nextPage()'>
<div ng-repeat="app in applications">{{app.name}}</div>
</div>
In my controller,
$scope.nextPage = function(){
console.log("nextPage");
$scope.page++;
.....
}
nextPage() method is called only once. It's not triggering after the first time.
What am I doing wrong ?
Try using this example
html
<div ng-app='myApp' ng-controller='DemoController'>
<div infinite-scroll='loadMore()' infinite-scroll-distance='2'>
<img ng-repeat='image in images' ng-src='http://placehold.it/225x250&text={{image}}'>
</div>
</div>
script
<script>
var myApp = angular.module('myApp', ['infinite-scroll']);
myApp.controller('DemoController', function($scope) {
$scope.images = [1, 2, 3, 4, 5, 6, 7, 8];
$scope.loadMore = function() {
var last = $scope.images[$scope.images.length - 1];
for(var i = 1; i <= 8; i++) {
$scope.images.push(last + i);
}
};
});
</script>

binding selected item angularjs

I can not use selectedItem1 in the controller for some calculation in the following code:
<div class="col-md-3">
<label>Vendere:</label>
<select ng-model="selectedItem1" ng-options="(rosa.nome+' '+rosa.costo) for rosa in rose"></select>
</div>
with the following controller:
var app = angular.module('App', []);
app.controller('Ctrl', function($scope, $http){
$scope.budget0=60;
$scope.budget1=$scope.budget0 - $scope.selectedItem1.someprop;
}
I can understand why? Where i'm wrong?
I guess you need to give initial value to selectedItem1 Check out following example will help you:--
var app = angular.module('App', []);
app.controller('Ctrl', function($scope, $http) {
$scope.rose = [{
'nome': "abc",
'costo':110
}, {
nome: "xyz",
costo: 10
}];
$scope.selectedItem1 = $scope.rose[0];// default selected item
$scope.budget0 = 60;
$scope.budget1 = $scope.budget0 - $scope.selectedItem1.costo;
$scope.change = function(){
$scope.budget1 = $scope.budget0 - $scope.selectedItem1.costo;
console.log($scope.selectedItem1);
console.log($scope.budget0 +"-"+ $scope.selectedItem1.costo +"="+$scope.budget1);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="App" ng-controller="Ctrl">
<select ng-model="selectedItem1" ng-change="change()"
ng-options="(rosa.nome+' '+rosa.costo) for rosa in rose"></select>
<br/>
{{"budget1 == "+budget1}}
</div>

How to print (on ng-click) an item of a ng-repeat outside the loop?

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>

Resources