Last active
August 29, 2015 14:05
-
-
Save joshdmiller/0ddd241b044d8458d513 to your computer and use it in GitHub Desktop.
Code Samples from the Article "The AngularJS Way"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul class="messages" id="log"> | |
</ul> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.ajax({ | |
url: '/myEndpoint.json', | |
success: function ( data, status ) { | |
$('ul#log').append('<li>Data Received!</li>'); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.directive( 'myDirective', function () { | |
return { | |
template: '<a class="btn">Toggle me!</a>', | |
link: function ( scope, element, attrs ) { | |
var on = false; | |
$(element).click( function () { | |
if ( on ) { | |
$(element).removeClass( 'active' ); | |
} else { | |
$(element).addClass( 'active' ); | |
} | |
on = !on; | |
}); | |
} | |
}; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="messages"> | |
<div class="alert" ng-repeat="entry in log"> | |
{{ entry.msg }} | |
</div> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul class="messages"> | |
<li ng-repeat="entry in log">{{ entry.msg }}</li> | |
</ul> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$http( '/myEndpoint.json' ).then( function ( response ) { | |
$scope.log.push( { msg: 'Data Received!' } ); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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; | |
}; | |
} | |
}; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<a href="/hello" when-active>Hello</a> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment