What is the best approach to manipulate the DOM after a partial view loads in AngularJS?
If I were using jQuery, I could utilize
$(document).ready(function(){
// do stuff here
}
However, with Angular, specifically when working with partial views, how can this be achieved? As an example, consider the following simple non-interactive Angular app (with HTML and JS on the same page source):
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Angular Question</title>
</head>
<body data-ng-app="demoApp">
<div data-ng-view=""></div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script>
var app = angular.module('demoApp', []);
var controller = {
demoController: function ($scope, demoFactory) {
$scope.fruits = demoFactory.getFruits();
}
};
var factory = {
demoFactory: function () {
var fruits = ['apples', 'bananas', 'cherries'];
var factory = {
getFruits: function () {
return fruits;
}
};
return factory;
}
}
function appRoute($routeProvider) {
$routeProvider
.when('/step-1',
{
controller: 'demoController',
templateUrl: 'partial.html'
})
.otherwise({ redirectTo: '/step-1' });
};
app.config(appRoute);
app.factory(factory);
app.controller(controller);
</script>
</body>
</html>
This app includes the following partial:
<ul>
<li data-ng-repeat="fruit in fruits">{{fruit}}</li>
</ul>
In this scenario, if I wanted to apply a "active" class to the first list item after the partial view finishes loading, how would I achieve this?