기본 옵션이 있는 AngularJS Directive
이제 막 AngularJs로 시작해서 몇 가지 오래된 jQuery 플러그인을 Angular 디렉티브로 변환하는 작업을 하고 있습니다.속성에서 옵션 값을 지정하여 재정의할 수 있는 내 (element) 지시문에 대한 기본 옵션 집합을 정의합니다.
다른 사람의 방법을 찾아봤는데 angular-ui 라이브러리에서 ui.bootstrap.pagation이 비슷한 작업을 하는 것 같습니다.
먼저 모든 기본 옵션이 상수 개체로 정의됩니다.
.constant('paginationConfig', {
itemsPerPage: 10,
boundaryLinks: false,
...
})
그리고 a.getAttributeValue
유틸리티 기능은 디렉티브컨트롤러에 연결되어 있습니다.
this.getAttributeValue = function(attribute, defaultValue, interpolate) {
return (angular.isDefined(attribute) ?
(interpolate ? $interpolate(attribute)($scope.$parent) :
$scope.$parent.$eval(attribute)) : defaultValue);
};
마지막으로, 이것은 링크 함수에 사용되며, Atribut을 읽습니다.
.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
function - 설정되지 않은 경우 속성을 읽습니다.기본값으로 채웁니다.
.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를 사용하고 있습니다.JS v1.5.10에서는 컴파일 함수가 디폴트 속성값을 설정하는데 꽤 잘 기능한다는 것을 알게 되었습니다.
주의사항:
attrs
raw DOM Atribute 값은 항상 다음 중 하나입니다.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: ...
}
});
언급URL : https://stackoverflow.com/questions/18784520/angularjs-directive-with-default-options
'prosource' 카테고리의 다른 글
export declare class Actions에서 declare는 무엇을 합니까? (0) | 2023.02.12 |
---|---|
여기서 woocommerce 순서는 wordpress 데이터베이스에 배치됩니다. (0) | 2023.02.12 |
wordpress에서 id로 게시물 존재 여부 (0) | 2023.02.12 |
카운터에서 디스커스 반응을 제거하는 방법 (0) | 2023.02.12 |
인수를 ajax onreadystatechange 콜백으로 전달하시겠습니까? (0) | 2023.02.12 |