Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. .constant('paginationConfig', {
  2. itemsPerPage: 10,
  3. boundaryLinks: false,
  4. ...
  5. })
  6.  
  7. this.getAttributeValue = function(attribute, defaultValue, interpolate) {
  8. return (angular.isDefined(attribute) ?
  9. (interpolate ? $interpolate(attribute)($scope.$parent) :
  10. $scope.$parent.$eval(attribute)) : defaultValue);
  11. };
  12.  
  13. .directive('pagination', ['$parse', 'paginationConfig', function($parse, config) {
  14. ...
  15. controller: 'PaginationController',
  16. link: function(scope, element, attrs, paginationCtrl) {
  17. var boundaryLinks = paginationCtrl.getAttributeValue(attrs.boundaryLinks, config.boundaryLinks);
  18. var firstText = paginationCtrl.getAttributeValue(attrs.firstText, config.firstText, true);
  19. ...
  20. }
  21. });
  22.  
  23. .directive('pagination', ['$parse', 'paginationConfig', function($parse, config) {
  24. ...
  25. controller: 'PaginationController',
  26. compile: function(element, attrs){
  27. if (!attrs.attrOne) { attrs.attrOne = 'default value'; }
  28. if (!attrs.attrTwo) { attrs.attrTwo = 42; }
  29. },
  30. ...
  31. }
  32. });
  33.  
  34. angular.module('myApp',[])
  35. .directive('myDirective', function(){
  36. return {
  37. template: 'hello {{name}}',
  38. scope: {
  39. // use the =? to denote the property as optional
  40. name: '=?'
  41. },
  42. controller: function($scope){
  43. // check if it was defined. If not - set a default
  44. $scope.name = angular.isDefined($scope.name) ? $scope.name : 'default name';
  45. }
  46. }
  47. });
  48.  
  49. .directive('myCustomToggle', function () {
  50. return {
  51. restrict: 'E',
  52. replace: true,
  53. require: 'ngModel',
  54. transclude: true,
  55. scope: {
  56. ngModel: '=',
  57. ngModelOptions: '<?',
  58. ngTrueValue: '<?',
  59. ngFalseValue: '<?',
  60. },
  61. link: {
  62. pre: function preLink(scope, element, attrs, ctrl) {
  63. // defaults for optional attributes
  64. scope.ngTrueValue = attrs.ngTrueValue !== undefined
  65. ? scope.ngTrueValue
  66. : true;
  67. scope.ngFalseValue = attrs.ngFalseValue !== undefined
  68. ? scope.ngFalseValue
  69. : false;
  70. scope.ngModelOptions = attrs.ngModelOptions !== undefined
  71. ? scope.ngModelOptions
  72. : {};
  73. },
  74. post: function postLink(scope, element, attrs, ctrl) {
  75. ...
  76. function updateModel(disable) {
  77. // flip model value
  78. var newValue = disable
  79. ? scope.ngFalseValue
  80. : scope.ngTrueValue;
  81. // assign it to the view
  82. ctrl.$setViewValue(newValue);
  83. ctrl.$render();
  84. }
  85. ...
  86. },
  87. template: ...
  88. }
  89. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement