我刚开始使用 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; }
},
...
}
});
ui.bootstrap.pagination
以更复杂的方式做事有什么想法吗?正在考虑如果使用 compile 功能,以后所做的任何属性更改都不会反映,但这似乎不是真的,因为在这个阶段只设置了默认值。猜猜这里必须做出一些权衡。
compile
中的@KenChatfield 您无法读取属性,应该对其进行插值以获取值(其中包含表达式)。但是,如果您只想检查属性是否为空 - 它可以在没有任何权衡的情况下为您工作(在插值属性将包含带有表达式的字符串之前)。
ui.bootstrap.pagination
示例中“interpolate”参数的作用,我发现了这个非常有用的示例:jsfiddle.net/EGfgH
attributes.foo = '["one", "two", "three"]'
而不是 attributes.foo = ["one", "two", "three"]
我正在使用 AngularJS v1.5.10,发现 preLink
compile function 可以很好地设置默认属性值。
只是提醒:
attrs 保存原始 DOM 属性值,这些属性值始终是未定义的或字符串。
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: ...
}
});
=?
从 1.1.x 开始可用true
或false
作为值,那么(我认为)您会希望使用例如$scope.hasName = angular.isDefined($scope.hasName) ? $scope.hasName : false;
来代替。=?
,但不适用于单向绑定,@?
。link
函数中设置默认值?根据我的理解,在link
期间分配应该避免$scope.$apply()
循环,不是吗?