기본적으로 지시문에서 "order-asc"및 "order-desc"클래스를 토글하려고합니다. 나는 지금 토글하는 부분을 폐기했다.데이터 바인딩 된 HTML 클래스를 지시문에 연결
describe('OfferList sorters', function() {
'use strict';
beforeEach(module('offerListSorters'));
describe('sorter directive', function() {
var element, scope, parentScope;
beforeEach(inject(function ($compile, $rootScope){
parentScope = $rootScope.$new();
element = angular.element('<sorter order-by="location">{{2+2}}</sorter>');
$compile(element)(parentScope);
scope = element.isolateScope();
parentScope.$digest();
}));
it('should contain 4', function() {
expect(element.html()).toBe('4');
});
it('should have asc/desc state on scope', function(){
expect(scope.isAscending).toBeDefined();
});
it('should have class order-asc by default', function() {
expect(element).toHaveClass('order-asc');
});
});
});
I 다음 지시문이 있습니다 :
나는 다음과 같은 검사를
offerListSorters.directive('sorter', [function(){
return {
scope: {},
controller: function($scope, $element, $attrs, $transclude) {
$scope.isAscending = true;
},
link: function($scope, iElm, iAttrs, controller) {
iElm.addClass('order-{{isAscending ? \'asc\' : \'desc\'}}');
}
};
}]);
내가 마지막 테스트가 실패 것을 찾을 수 있습니다. {{}}
바인딩은 그대로 클래스 속성에 추가됩니다. 내가 링크 기능의 끝에 $scope.$digest();
를 추가 해봤
iElm.attr('ng-class', 'isAscending ? \'asc\' : \'desc\'');
iAttrs.$set('ngClass', 'isAscending ? \'asc\' : \'desc\'');
: 나는 또한 지침에 다음 줄이없는 결과로 기능을 연결 시도했습니다. 하지만 아무데도 못 가봤 어.
here 또는 $ function. $ watch magic을 링크 기능으로 수행하여 해결할 수는 있지만 현재 시도하고있는 것이 왜 작동하지 않는지 이해하고 싶습니다.
나는 Some text'처럼 저의 분류기를 정의하고 싶습니다. 클래스를 처리하는 논리는 지시문에 의해 처리되어야합니다. 예를 들어 링크 1 대신 컴파일 함수를 사용해야합니까? –
Deiwin
중요한 점은 로직이 마크 업에서 볼 수 있다는 것입니다. 어쨌든 다른 옵션이 있습니다. 정렬 순서가 오름차순인지 또는 내림차순인지 정의하는 것은 무엇입니까? 주문한거야? – Narretz
아니요. 격리 범위에 정의 된 토글 가능한 값입니다. 지시문의 요소에는 isAscending 값을 토글하는 클릭 바인딩이 있습니다. – Deiwin