Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. <ul ng-controller="mainCtl">
  2. <li inner="{item: item}" ng-repeat="item in items" ng-class="{sel: inner.selected}">
  3. {{ item.Name }}
  4. </li>
  5. </ul>
  6.  
  7. directive('inner', [
  8. function() {
  9. return {
  10. restrict: 'A',
  11. scope: {
  12. inner: '='
  13. },
  14. link: function(scope, element, attr) {
  15. scope.inner.selected = false;
  16. element.bind('click', function(e) {
  17. scope.inner.selected = !scope.inner.selected;
  18. });
  19. }
  20. };
  21. }]);
  22.  
  23. <ul ng-controller="mainCtl">
  24. <li ng-click="selected=!selected" ng-repeat="item in items" ng-class="{sel: selected}">
  25. {{ item.Name }}
  26. </li>
  27. </ul>
  28.  
  29. angular.module('test', []).
  30. controller('mainCtl', ['$scope',
  31. function($scope) {
  32. $scope.items = [{Name: 'obj1'}, {Name: 'obj2'}];
  33. console.log($scope);
  34. }]).
  35. directive('inner', [
  36. function () {
  37. return {
  38. restrict: 'A',
  39. scope: {
  40. inner: '='
  41. },
  42. link: function (scope, element, attr) {
  43. var el = angular.element(element);
  44. el.on('click', function (a, b, c) {
  45. if (el.hasClass('sel')) {
  46. el.removeClass('sel');
  47. } else {
  48. el.addClass('sel');
  49. }
  50. });
  51. }
  52. };
  53. }]);
  54.  
  55. app.service('selection', [function(){
  56. function selection(array){
  57. //constructor function that attaches or creates the selection logic for the array
  58. }
  59.  
  60.  
  61. this.create = function(input){
  62. return new selection(input);
  63. }
  64. }]);
  65.  
  66. app.controller('myCtrl', ['selection', '$scope', function(selection, $scope){
  67. $scope.array = [{...},{...}];
  68.  
  69.  
  70. //Creates a selection from the array
  71. $scope.selection=selection.create($scope.array);
  72. }])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement