Answer the question
In order to leave comments, you need to log in
What's the smartest way to interact with an array in a controller (dynamically)?
Hello everyone, I have the task of creating a grid and changing the block classes in it.
We have an array in the controller:
$scope.gridList = [
{ 'row': [{
"lg":"4",
"md":"4",
"sm":"6",
"xs":"12"
},{'row': {....}]
}]
$scope.switchOp = function (idV, classV) {
$scope.stilic.classView = classV; // заносим в спец блочек
// собираем нужный класс
$scope.menegerClass = 'col-'+classV+'-'+$scope[0].classV;
}
$scope.plus = function(){
if (this.item.lg >= 12) {return;}
this.item.lg++;
};
Logic in a nutshell, there is a list of rules (name, id, prefix) - descope, 1, lg. if it is active, then classV takes the value - prefix, and then goes to the ng-class construction rules, but how to take the value of the selected prefix from another array with ready-made grid data in order to use it in ng-class, in forich (when displaying all grid items, ng-repeat)
<section class="row" ng-repeat="list in gridList">
<div data-ng-repeat="item in list.row">{{item.lg}}</div>
</section>
Answer the question
In order to leave comments, you need to log in
solution:
add ng-class="function(item) to our blocks, i.e.
now with a function, use check
$scope.getClass = function(item) {
if (item.hasOwnProperty(cp)) {
className = 'col-' + cp + '-' +item[cp];
}
return
}
DEMO .
angular.module('toster', [])
.controller('GridCtrl', function() {
var vm = this;
var grids = [
{
xs: '12',
sm: '6',
md: '4',
lg: '4'
},
{
xs: '10',
sm: '8',
md: '6',
lg: '3'
}
];
var activeIndex = 0;
vm.activeGrid = grids[activeIndex];
vm.toggleGrid = function() {
switch (activeIndex) {
case 0:
vm.activeGrid = grids[++activeIndex];
break;
case 1:
vm.activeGrid = grids[--activeIndex];
break;
}
};
})
.directive('dynamicGrid', function() {
return {
scope: {
grid: '=dynamicGrid'
},
templateUrl: 'dynamicGrid.html',
link: function(scope, elem) {
scope.$watch('grid', function(grid) {
var gridCss = {};
angular.forEach(grid, function(value, key) {
gridCss['col-' + key + '-' + value] = true;
});
scope.gridCss = gridCss;
});
}
};
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question