Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .constant('paginationConfig', {
- itemsPerPage: 10,
- boundaryLinks: false,
- ...
- })
- this.getAttributeValue = function(attribute, defaultValue, interpolate) {
- return (angular.isDefined(attribute) ?
- (interpolate ? $interpolate(attribute)($scope.$parent) :
- $scope.$parent.$eval(attribute)) : defaultValue);
- };
- .directive('pagination', ['$parse', 'paginationConfig', function($parse, config) {
- ...
- controller: 'PaginationController',
- link: function(scope, element, attrs, paginationCtrl) {
- var boundaryLinks = paginationCtrl.getAttributeValue(attrs.boundaryLinks, config.boundaryLinks);
- var firstText = paginationCtrl.getAttributeValue(attrs.firstText, config.firstText, true);
- ...
- }
- });
- .directive('pagination', ['$parse', 'paginationConfig', function($parse, config) {
- ...
- controller: 'PaginationController',
- compile: function(element, attrs){
- if (!attrs.attrOne) { attrs.attrOne = 'default value'; }
- if (!attrs.attrTwo) { attrs.attrTwo = 42; }
- },
- ...
- }
- });
- angular.module('myApp',[])
- .directive('myDirective', function(){
- return {
- template: 'hello {{name}}',
- scope: {
- // use the =? to denote the property as optional
- name: '=?'
- },
- controller: function($scope){
- // check if it was defined. If not - set a default
- $scope.name = angular.isDefined($scope.name) ? $scope.name : 'default name';
- }
- }
- });
- .directive('myCustomToggle', function () {
- return {
- restrict: 'E',
- replace: true,
- require: 'ngModel',
- transclude: true,
- scope: {
- ngModel: '=',
- ngModelOptions: '<?',
- ngTrueValue: '<?',
- ngFalseValue: '<?',
- },
- link: {
- pre: function preLink(scope, element, attrs, ctrl) {
- // defaults for optional attributes
- scope.ngTrueValue = attrs.ngTrueValue !== undefined
- ? scope.ngTrueValue
- : true;
- scope.ngFalseValue = attrs.ngFalseValue !== undefined
- ? scope.ngFalseValue
- : false;
- scope.ngModelOptions = attrs.ngModelOptions !== undefined
- ? scope.ngModelOptions
- : {};
- },
- post: function postLink(scope, element, attrs, ctrl) {
- ...
- function updateModel(disable) {
- // flip model value
- var newValue = disable
- ? scope.ngFalseValue
- : scope.ngTrueValue;
- // assign it to the view
- ctrl.$setViewValue(newValue);
- ctrl.$render();
- }
- ...
- },
- template: ...
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement