<ul class="main-menu">
<li class="active">
<a href="#/home">Home</a>
</li>
<li>
<a href="#/menu1">Menu 1</a>
<ul>
<li><a href="#/sm1">Submenu 1</a></li>
<li><a href="#/sm2">Submenu 2</a></li>
<li><a href="#/sm3">Submenu 3</a></li>
</ul>
</li>
<li>
<a href="#/home">Menu 2</a>
</li>
</ul>
We would wp-wp-wp-wp-wp-activate.php.php.php.php.php it in jQuery using our application logic.
$('.main-menu').dropdownMenu();
<ul class="main-menu" dropdown-menu>
...
</ul>
This is a great feature of AngularJS. It eliminates the need for you to perform the DOM manipulations mentioned in the previous section. AngularJS will update your view automatically so that you don’t have! jQuery responds to events and updates content. It looks something like this:
$.ajax({
url: '/myEndpoint.json',
success: function ( data, status ) {
$('ul#log').append('<li>Data Received!</li>');
}
});
This is a picture of what it looks like:
<ul class="messages" id="log">
</ul>
This can be messy and difficult. AngularJS makes it possible.
$http( '/myEndpoint.json' ).then( function ( response ) {
$scope.log.push( { msg: 'Data Received!' } );
});
Our view could look something like this:
<ul class="messages">
<li ng-repeat="entry in log">{{ entry.msg }}</li>
</ul>
However, this is our alternative view:
<div class="messages">
<div class="alert" ng-repeat="entry in log">
{{ entry.msg }}
</div>
</div>
Because we have a separation, it is possible to do test-driven development in AngularJS! Let’s take, for example, a very simple directive that indicates in our menu our current route. In the view of your application, you can specify what you want.
<a href="/hello" when-active>Hello</a>
it( 'should add "active" when the route changes', inject(function() {
var elm = $compile( '<a href="/hello" when-active>Hello</a>' )( $scope );
$location.path('/not-matching');
expect( elm.hasClass('active') ).toBeFalsey();
$location.path( '/hello' );
expect( elm.hasClass('active') ).toBeTruthy();
}));
We can also confirm the failure of our test when we run it. Now is the time to create your directive.
.directive( 'whenActive', function ( $location ) {
return {
scope: true,
link: function ( scope, element, attrs ) {
scope.$on( '$routeChangeSuccess', function () {
if ( $location.path() == element.attr( 'href' ) ) {
element.addClass( 'active' );
}
else {
element.removeClass( 'active' );
}
});
}
};
});
This is the most common pattern I see. We need a button that can be toggled. This example is somewhat contrived to show more complex cases that can be solved in the exact same way.
.directive( 'myDirective', function () {
return {
template: '<a class="btn">Toggle me!</a>',
link: function ( scope, element, attrs ) {
var on = false;
$(element).click( function () {
on = !on;
$(element).toggleClass('active', on);
});
}
};
});
This directive can be changed (even in very complex cases!) You can do it even more simply:
.directive( 'myDirective', function () {
return {
scope: true,
template: '<a class="btn" ng-class="{active: on}" ng-click="toggle()">Toggle me!</a>',
link: function ( scope, element, attrs ) {
scope.on = false;
scope.toggle = function () {
scope.on = !scope.on;
};
}
};
});