티스토리 뷰
AngularJS: Implementing a Reusable Toggle Chevron with MVC Architecture
JoyJaewon 2024. 5. 29. 22:28
In this blog post, we'll delve into how to implement a reusable toggle chevron feature in AngularJS, which is particularly handy for collapsible panels or dropdowns. This example will follow the Model-View-Controller (MVC) pattern, ensuring our code is both manageable and scalable. We will create a controller that handles the logic, use a directive for our view, and maintain the state in our model.
AngularJS MVC Structure and Implementation
The MVC architecture separates concerns, making our applications easier to manage and extend:
- Model: Manages data and rules. Here, the model will keep track of whether sections are expanded or collapsed.
- View: Displays data (model) and sends user actions (events) to the controller.
- Controller: Updates the model based on user actions captured in the view and updates the view in response to model changes.
Step-by-Step Implementation
1. HTML Structure
We start by defining our HTML structure that will utilize our AngularJS components. We'll pass chevronId through to our AngularJS code, ensuring we can uniquely identify and control our toggle chevron.
<div ng-app="toggleApp" ng-controller="ToggleController">
<div>
<a ng-click="toggleChevron()"
class="btn btn-chevron_icon"
data-bs-toggle="collapse"
href="#{{chevronId}}"
role="button"
aria-expanded="{{isTableExpanded}}"
aria-controls="{{chevronId}}"
ng-class="{'disabled': itemCollection.length == 0, 'btn-chevron_icon--disabled': itemCollection.length == 0}">
<i ng-class="{'fa fa-chevron-up': isTableExpanded, 'fa fa-chevron-down': !isTableExpanded}"></i>
</a>
Address
</div>
</div>
2. AngularJS Controller
The controller will handle the logic for toggling the chevron and listening to changes in the collection that might affect the visibility and state of the chevron.
angular.module('toggleApp', [])
.controller('ToggleController', ['$scope', '$attrs', function ($scope, $attrs) {
$scope.chevronId = $attrs.chevronId;
$scope.isTableExpanded = true;
$scope.toggleChevron = function() {
$scope.isTableExpanded = !$scope.isTableExpanded;
};
$scope.$watch('itemCollection.length', function (newLength) {
$scope.isTableExpanded = newLength > 0;
});
}]);
3. Explanation of the Code
- The ToggleController initializes with a default expanded state (isTableExpanded = true).
- toggleChevron is a function bound to the click event of the chevron link, which toggles the state of isTableExpanded.
- A $watch is set up on itemCollection.length to automatically collapse the chevron if the collection is empty, demonstrating responsive UI updates based on data changes.
By encapsulating the toggle logic in a directive and handling the state through a controller, this AngularJS implementation follows the MVC pattern, ensuring that each aspect of the application is properly isolated but works cohesively. This approach enhances maintainability and scalability, allowing other developers to easily understand and reuse components across the application.
- Total
- Today
- Yesterday
- Desk Setup
- bootstrap5
- handleNullData
- CSS white space issue
- Responsive Troubleshooting
- JSON-CSV-Conversion
- ImportCSV
- CSS
- frontend
- $scope.$applyAsync()
- Next.js
- angularjs
- promise.all()
- junior frontend developer
- BEM-like-Naming
- developer tools
- React.js
- CSR
- CSV Validation
- scrollabe table
- #노개북 #노마드코더 #개발자북클럽 #클린코드
- SSR
- UX
- Bootstrap5 tab
- responsive design
- BEM
- responsive table
- orderByFilter
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |