What is the method to deactivate the date field by using an attribute?

In an attempt to disable a field using an attribute without utilizing the directive scope property, I am seeking a way to disable my field with the type 'date'.

Check out my code snippet below:

http://plnkr.co/edit/eJDRocLYkr8Krh84vFKY?p=preview

 var app = angular.module('plunker', []);

    app.controller('MainCtrl', function($scope) {
      $scope.name = 'World';
      $scope.clickb=function(){
        alert('dd')
        $scope.disabletest=true;
      }
    });

    app.directive('test',function(){
      return {
        restrict :'E',
        scope:{},
        template:'<input type="date" ng-disable="disabletest">',
        link:function(s,e,a){

        }

      }
    })

The value of the disable attribute in the directive is used to disable the field upon button click.

My goal is to use the attribute property to disable the input field of type 'date' upon clicking a button.

Answer №1

Here's a suggestion: Modify the ng-disable to ng-disabled, and introduce a scoped variable.

var myApp = angular.module("app",  []);

myApp.controller('ctrl', function($scope){
  $scope.disableInput = false;
  $scope.toggleDisable=function(){
    $scope.disableInput=!$scope.disableInput;
  };
});

myApp.directive('customDirective',function(){
  return {
    restrict :'E',
    template:'<input type="date" ng-disabled="disable">',
    link:function(s,e,a){
      a.$observe('disable', function(value) {
      s.disable = s.$eval(a.disable);
    });
  }
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <button ng-click='toggleDisable()'>btn</button>
  <custom-directive disable='{{disableInput}}'></custom-directive>
</div>

Answer №2

Needed

 ng-disabled="expression"

index.html

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b4d5dad3c1d8d5c69adec7f4859a809acc">[email protected]</a>" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>
    <script src="app.js"></script>
  </head>

  <body >
    <div ng-controller="MainCtrl">
    <p>Hey there, {{name}}!</p>
    <button ng-click='clickb()'>btn</button>
    <input type="date" ng-disabled='disabletest'>    
    </div>

  </body>

</html>

When creating a directive, remember to define a property to access the value of disabletest

Answer №3

Instead of using $parent, it is recommended to follow Jimbrooism's suggestion and change the scope for a better solution. The following code demonstrates how to achieve this without altering the scope:

template:'<input type="date" ng-disabled="$parent.disabletest">',

http://plnkr.co/edit/ZBKv63aroXrao7GO9P2u?p=preview

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

What are the steps to reset a JavaScript game?

I'm currently working on a JavaScript game project. Once the game is finished, I'd like to give players the option to go back to the main menu by clicking a button, then start a new game by clicking play again. However, I've run into an issu ...

Tips for Removing Padding in Material UI Container Component

I'm currently working on creating a hero banner using the material-ui framework and I've encountered an issue. Here's what I have so far: https://i.stack.imgur.com/UXTDY.png However, I'm facing an irritating problem with left and rig ...

React function does not provide a return value

In my function, I am calculating the current sum of elements. Within my Api, there is a method called Getcoin that works correctly when I log each element during the foreach cycle. However, I am not getting any results in the return statement or console.lo ...

What causes the strings in an array to be split into individual characters when using the jquery each() function?

After successfully loading jquery, I have initialized an array (object) with various strings: window.stack = [ "header", "nav", ".content", "footer" ]; My intention was to loop through this array using jquery's each() function to retrieve ea ...

Tips for retrieving JSON data using ajax with jPut

Recently, I stumbled upon a handy jQuery plugin called Jput that allows for easy appending of JSON data to HTML content. You can check it out here. However, I am curious about the process of sending and retrieving JSON data via AJAX. Any insights or guida ...

A fresh inquiry: jQuery custom plugin callback

Many individuals inquire about plug-ins and callbacks, and after reading numerous posts on the topic, I have managed to create a simple hide/show accordion type plugin for FAQs. While I am pleased with its functionality, as I continue to learn, some aspe ...

Uploading images with AngularJS and PHP

Despite having successfully done this in the past, I am currently facing an issue and I am completely stumped as to what could be causing it. The problem I am encountering is related to uploading an image using Angularjs and passing it to a PHP file. Stra ...

An effective method for retrieving elements from a one-dimensional array using nested loops

I have a vector containing data that needs to be stored in the following order: cAge[0] = 'Age (1) (1)'; cAge[1] = 'Age (1) (2)'; cAge[2] = 'Age (1) (3)'; cAge[3] = 'Age (2) (1)'; cAge[4] = 'Age (2) (2)&apo ...

Exploring the benefits of incorporating the vendor folder in website development

I'm embarking on the journey of creating a website from scratch with python, django, and bootstrap. I've noticed that it's common practice to store js, css, img, and fonts in a folder named vendor, like this: /static/js/vendor/bootstrap/boo ...

Using Angular's ng-href directive in conjunction with the Split method

I am encountering an issue with a recursive variable that has a specific value path = <d:URL>http://www.google.com, Google</d:URL> My goal is to bind this path into an Angular component. I have attempted to use the following method to bind th ...

Specialized Directive undermines the functionality of ngRepeat

My initial worry was that I wouldn't be able to replicate the issue, yet, I was able to replicate the problem: jsfiddle The scenario presented is straightforward, but mirrors exactly what I'm facing. In essence, I have a directive that doesn&ap ...

Python executing Javascript code syntax

I am currently working on a project where I need to execute javascript code within Python, but I seem to be running into syntax issues. It seems like the problem lies with the use of single and double quotation marks. Here's a more detailed explanati ...

Creating a nested JSON structure by fetching data from a remote source

i'm looking to populate the json file located at through a get request. This json contains music from various churches in different cities, with data organized into multiple levels. I want to parse this data using jquery and utilize hidden div elemen ...

Guide for manually initiating the mouseleave event on a smartphone or tablet

Is there a way to change the color of a link to orange when it's hovered over? On mobile devices, the link should turn orange when touched and stay that way until the user clicks away. I'd like to manually trigger the mouseout event to remove th ...

Answer processing for the reminder dialog is underway

When I send a proactive message to a user, I want to initiate a 'reminder dialog'. The dialog is displayed, but when processing the response it goes back to the main dialog. This is how I currently set up my bot: const conversationState = new C ...

AngularJS directive failing to trigger event binding within the link function

Here is a plunker that you can refer to. In my project, I have developed two simple element directives, named incButtonOne and incButtonTwo. These directives are designed to track and display the number of times they have been clicked. Each directive has ...

Switch Selector for React Native

Although it may seem simple, I'm having trouble getting a function to automatically switch between the "user" and "business" options on the switch selector without requiring manual input from the user. I attempted to use: const switchRef = useRef(nu ...

How to easily toggle all checkboxes within a Vue.js checkbox group

I'm struggling with manipulating an Array of checkboxes that are grouped into parent and child elements. I need to automatically check all child checkboxes when the parent is checked, uncheck them if the parent is unchecked, and update their states in ...

Can you provide the keycodes for the numpad keys: "/" and "." specifically for the libraries I am utilizing or any other library that does not overlook them?

I've hit a roadblock with my Chrome Extension development. Despite using two different methods to add keyboard functionality, the keys "/" for divide and "." for decimal on the keypad are just not registering. I've attempted to tackle this issue ...

How can Navbar values in a React application be dynamically updated depending on the user's login status?

As someone new to React, I am exploring the correct approach to building a Navbar that dynamically displays different links based on a user's login status. When a user logs in, a cookie loggedIn=true is set. In my Navbar component, I utilize window.se ...