Skip to content

Instantly share code, notes, and snippets.

@whisher
Created September 23, 2013 13:34
Show Gist options
  • Select an option

  • Save whisher/6670471 to your computer and use it in GitHub Desktop.

Select an option

Save whisher/6670471 to your computer and use it in GitHub Desktop.
<!doctype html>
<html ng-app="myApp">
<head>
<meta charset="utf-8">
</head>
<style>
.active{
color:red;
}
</style>
<body>
<div ng-controller="myCtrl">
<ul>
<li data-ng-repeat="user in ns.users">
<div selected-user-box="{{user.id}}" class="box-select-user">
<h2>{{user.name}}</h2>
</div>
</li>
</ul>
<b>{{ns.test}}</b>
</div>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.angularjs.org/1.0.8/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.factory('Users',function(){
return [{id:1,name:'first'},{id:2,name:'second'},{id:3,name:'third'}]
});
app.directive('selectedUserBox', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('click', function() {
$(this).parent().parent().find('div').removeClass('active');
$(this).addClass('active');
scope.ns.test = attrs.selectedUserBox;
scope.$apply();
});
}
};
});
app.controller('myCtrl',function($scope,Users){
$scope.ns = {};
$scope.ns.test = 0;
$scope.ns.users = Users;
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment