Utilize the directive solely when the preceding element is not in a disabled state

Situation:

Here is an example of an Inputfield:

 <ion-input class="item item-input">
      <ion-label>Field</ion-label>
           <input type="text" ng-disabled="someCondition" model="somemodel" />
           <directive-element></directive-element>
 </ion-input>

I am currently using a directive to replace the "directive-element" with a button that triggers a function on click. However, I am facing difficulty detecting whether the input is disabled or not. I have tried accessing it through element[0], but without success.

.directive('directiveElement', function () {   
    return {
        restrict: 'AE',
        replace: true,
        template: function (element, attrs) { 
             var input = ?????.
             if(input.disabled == false){             
                return "<button ng-click='myFunction()'></button>"; 
             }else{
                //Do not show the button 
              }         
        },
        link: function (scope, element, attrs) {
                scope.myFunction(){
                    //perform some logic
                }  


        };
    }

})

EDIT:

template: function (element, attrs) {
    if (element[0].previousElementSibling != null && element[0].previousElementSibling.tagName == 'INPUT' && element[0].previousElementSibling.disabled == true) {
          //this approach seems to work somewhat, but I still see the button...
    }
},

I prefer not to use jQuery and would like to avoid Jqlite if possible.

Thank you!

Answer №1

To handle the condition, you can create a separate third function to check it:

template: function (element, attrs) {            
        return "<button ng-click='checkConditionAndExecuteFunction(someCondition)'></button>";             
},
link: function (scope, element, attrs) {
      scope.executeFirstFunction(){
           //perform some operations
      } 

      scope.executeSecondFunction(){
           //perform different operations
      }  

      scope.checkConditionAndExecuteFunction(condition){
          if (condition) {
             scope.executeFirstFunction();
          } else {
             scope.executeSecondFunction();
          }
      }

};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript in fullscreen mode for Internet Explorer

I'm trying to make sure that this code snippet... $('#gatewayDimmer').width($('html').width()); $('#gatewayDimmer').height($('html').height()); $('#gatewayDimmer').css('display','block& ...

Storing JSON values dynamically

On a PHP webpage, I am sending values using JSON. $result = mysql_query("SELECT * FROM user_info"); $i=1; while($row = mysql_fetch_array($result, MYSQL_NUM)) { if(is_array($row) && count($row)>0) { $res[&a ...

The error message that is popping up on Windows when running `npm start` is

Hey there! I'm having an issue with my Windows 10 installation and Mean. After installing express, I tried to start npm using the command "npm start" but encountered the following error: C:\>npm start npm ERR! Windows_NT 6.3.9600 npm ERR! arg ...

Is there a way for me to discover the identity of the victor?

There are 4 divs that move at different, random speeds each time. I am trying to determine which one is the fastest or reaches the goal first. Additionally, there is a betting box where you can choose a horse to bet on. I need to compare the winner with my ...

Display a Three-js Scene in an HTML5 Canvas

Having some trouble rendering a Three-js scene in a canvas on my index page. I've got the basic templates set up for both the canvas and the three-js scene, but how do I actually render the scene on the canvas? index.html <!DOCTYPE html> <h ...

Three.js is capable of displaying a scene background, but it does not currently show any

I feel like I'm at my wit's end. I've been struggling to make anything render in this simple three.js program and it's driving me insane. I've tried everything - adding cubes, copying geometries, adjusting lights, moving the camera ...

What is the best way to notify an XML file using jQuery?

I am encountering an issue with a login api when trying to send an xml document. The error occurs when including '<?', but I need to send the whole XML with it. Can someone assist me in sending the complete XML using a different method or type ...

Minimize/Maximize Swagger Response Model Class View

After successfully integrating Swagger API documentation with my rest services, I encountered a challenge. The Swagger page appears too lengthy due to the numerous response classes in my project, requiring users to scroll extensively to find information. ...

Tips for transferring an MVC model to a UI-bootstrap modal

Trying to implement an Angular/bootstrap modal for editing MVC ApplicationUser scaffolded views. Came across jquery examples but want to stick with angular-ui or plain bootstrap for consistency in modals. Unclear on how the MVC controller is being called f ...

Troubleshooting the Gutter Problem in jQuery Isotope and Masonry

Currently, I am utilizing the Isotope jQuery plugin. While it is a fantastic tool, I am encountering a minor issue with aligning items in masonry mode. The width of my container is 960px, and I aim to have 4 items perfectly aligned as if they were adhering ...

Incorporate Material Design Lite and AMP into your Angular 5 project for a sleek

Looking to develop an e-commerce progressive web app using angular 5. Wondering how to incorporate AMP with angular 5 in Google Material Design Lite. If not viable, what are some alternative options worth considering? ...

Tips for setting background colors as a prop for Material UI cards in React JS

Currently utilizing the Material UI next framework to construct a wrapper for the card component. This customized wrapper allows for personalization of the component. I have successfully extended the component so that the title and image within the card ca ...

PHP header malfunctioning post AJAX request triggered by JavaScript

Hey there, I have a query that might sound silly to some, but I'm curious if it's feasible to utilize the header function within a php file when receiving an AJAX response. In my scenario, I'm working on a login form where I use AJAX to com ...

Update the label on a webpage using Selenium's JavaScriptExecutor

We are currently working on a Japanese web application and are experimenting with a unique method to convert Japanese labels into English. Here is the approach we are using (though unsure if it's correct): Approach (using Selenium): Open the webpag ...

Oops! Hardhat Test Error "Error: Virtual Machine Exception occurred while processing transaction: reverted with reason 'Please deposit additional funds'."

Encountering an issue with the following error message: Error: VM Exception while processing transaction: reverted with reason string 'deposit more' in the Hardhat Test.js file Test.js -> it("should be able to withdraw if no one appl ...

Creating duplicates of table rows featuring Glyphicon and Bootstrap for an interactive "Data Live Search" dropdown

After successfully creating rows by clicking the plus glyphicon, the Bootstrap dropdown (with live search) is not functioning in the 2nd row and subsequent rows (after cloning). Please see the provided code snippet below. Any guidance on how to resolve th ...

Preventing the window from resizing while an AJAX query is in progress

I have a script in jQuery that serves as a search tool and also has the capability to resize an element to match the screen height minus 40 pixels whenever the window is resized. However, I am looking for a way to turn off the resizing feature when a searc ...

Filtering input that is compatible with Firefox

Attempting to restrict certain special characters (excluding "_" and "-") from being used in a text input field has been a bit of a challenge. I initially tried using regex filtering by pattern, but it didn't work as expected. Then I turned to dynami ...

Utilizing a variable beyond the confines of the script tag

Is there a way to assign the value of my variable place to my variable address, even though they are not in the same script tag? I want to bind the value and utilize it for fetching data. When I log my variable place, I can see the address, but I am unsure ...

Having Trouble with Updating Variables in AngularJS Service

I am faced with a challenge involving a series of images displayed side by side. My goal is to determine which image has been clicked, retrieve the relevant information, and display it in a modal window. HTML Markup <section class="portfolio-grid ...