繁体   English   中英

具有默认选项的 AngularJS 指令

[英]AngularJS Directive with default options

我刚开始使用 AngularJS,并且正在努力将一些旧的 jQuery 插件转换为 Angular 指令。 我想为我的 (element) 指令定义一组默认选项,可以通过在属性中指定选项值来覆盖它们。

我环顾了一下其他人的做法,在angular-ui库中, ui.bootstrap.pagination似乎做了类似的事情。

首先,所有默认选项都定义在一个常量对象中:

.constant('paginationConfig', {
  itemsPerPage: 10,
  boundaryLinks: false,
  ...
})

然后将getAttributeValue实用函数附加到指令控制器:

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);
        ...
    }
});

对于想要替换一组默认值的标准设置,这似乎是一个相当复杂的设置。 还有其他常见的方法吗? 或者总是以这种方式定义诸如getAttributeValue之类的实用程序函数并解析选项是否正常? 我很想知道人们对这项共同任务有什么不同的策略。

另外,作为奖励,我不清楚为什么需要interpolate参数。

使用=? 指令范围块中属性的标志。

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';
      }
    }
  });

您可以使用compile功能 - 如果未设置属性,则读取属性 - 用默认值填充它们。

.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; }
    },
        ...
  }
});

我正在使用 AngularJS v1.5.10 并发现preLink编译功能可以很好地设置默认属性值。

只是提醒:

  • attrs保存原始DOM 属性值,这些属性值始终是undefined的或字符串。
  • scope保存(除其他外)根据提供的隔离范围规范( = / < / @ / 等)解析的 DOM 属性值。

节选片段:

.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: ...
  }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM