![]() |
In AngularJS, passing input to a custom directive allows the customization of components within an application. Directives can receive input via attributes, expressions, or controller functions, allowing for dynamic behavior and data binding. In this article, we will explore two different approaches to pass input to a custom directive. Table of Content Steps to Configure the AngularJS ApplicationsThe below steps will be followed to configure the AngularJS Applications: Step 1: Create a new folder for the project. We are using the VSCode IDE to execute the command in the integrated terminal of VSCode. mkdir custom-directive Step 2: Create the index.html file in the newly created folder, we will have all our logic and styling code in this file. Approach 1: Using Attribute BindingIn this approach, we are using attribute binding to pass input to a custom directive in AngularJS. The directive is defined with restrict: ‘A’, and the input value is passed through the myDirective attribute. Within the directive’s link function, we watch for changes in the myDirective value and update the element’s text. Syntax:<div my-directive="name"></div> Example: The below example uses Attribute Binding to pass input to a custom directive in AngularJS.
Output: Approach 2: Using ServicesIn this approach, we are using an AngularJS service to perform communication between the controller and the directive. The service (inputService) stores the user input and provides methods to manipulate it. The controller updates the service with the user input, and the directive listens for changes in the service’s data and updates the DOM. Syntax:app.service('serviceName', function() { Example: The below example uses Services to pass input to a custom directive in AngularJS.
Output: |
Reffered: https://www.geeksforgeeks.org
AngularJS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |