$scope and counting children of an element in AngularJS -
i'm pretty new angularjs, , have jquery background influence way of thinking problem.
in order dom manipulation through transclude
directive (i.e. adding specific class) need know how many children (or maybe siblings) has generic element.
mean set class on children, based on algorithm counts number of children themselves.
this tried far
var main = angular.module("main",[]); function utilities(){ this.consolescope = function($scope){ return $scope.children().length; }; } main.service("utilities",[utilities]); main.controller("prova",["$scope","utilities",function($scope,utilities){ var self = this; self.consolescope = function(){ return utilities.consolescope($scope); }; }]);
but if runs without errors, doesn't retrieve information wanted. can comprehend not right way this, can't see other way. try?
so you've mixed application logic dom logic. ideally when talking children you'd assigning or creating these based upon data set or collection. e.g.
//in controller $scope.data = somedataset;
from there implement algorithm based upon data set.
//still in controller $scope.algorithm = function(data){ ... implement logic ... // e.g. return data.length > 5; }
now in ui mark use ng-class
, expression assign class on element needs class. controller shouldn't know classes.
<div ng-class="(algorithm(data)) ? 'trueclass' : 'falseclass'" ></div>
this simple implementation can extend pretty easily.
Comments
Post a Comment