DOM manipulacje w angularjs nie powinny znajdować się w kontrolerze, usług itp ... ale to powinno być w tylko jedno miejsce dyrektyw ...
jeśli chcesz manipulować DOM należy użyć dyrektywy i dokonać manipulacji tam ...
tutaj jest kilka dobrych artykułów na temat DOM manipulacji w angularjs ...
Best Practice - Dom Manipulations
DOM Manipulation in AngularJS — Without jQuery
teraz spróbujmy stworzyć dyrektywę jak chcesz. Wygląda na to, że chcesz manipulować elementem, wybierając go za pomocą swojej klasy. Ok nie ma problemu więc musimy stworzyć dyrektywę, która ma restrict:'C'
oznacza CLASS ...
tutaj jest nasza deklaracja dyrektywy ... (wersja opisowy, aby pokazać wszystko)
app.directive('myClass',function(){
// Runs during compile
return {
// name: '',
// priority: 1,
// terminal: true,
// scope: {}, // {} = isolate, true = child, false/undefined = no change
// controller: function($scope, $element, $attrs, $transclude) {},
// require: 'ngModel', // Array = multiple requires, ? = optional,^= check parent elements
restrict: 'C', // E = Element, A = Attribute, C = Class, M = Comment
// template: '',
// templateUrl: '',
// replace: true,
// transclude: true,
// compile: function(tElement, tAttrs, function transclude(function(scope, cloneLinkingFn){ return function linking(scope, elm, attrs){}})),
link: function($scope, iElm, iAttrs, controller) {
console.log('Here is your element', iElm);
// DO SOMETHING
}
};
});
tutaj jest PLUNKER. ..
Trzeba przejść przez [? __How mogę „myśleć w angularjs” jeśli mam jQuery tło __] (http://stackoverflow.com/questions/14994391/how- do-i-think-in-angularjs-if-i-have-a-jquery-background). – Satpal