Angular: A guide to binding to the required/ngRequired attribute

There is a directive that may or may not be required, and it can be used in two different ways.

<my-foo required></my-foo>

or

<my-foo ng-required="data.value > 10"></my-foo>

Even though require and ngRequire are essentially the same thing, you would expect the directive to work like this

HTML:

<my-foo ng-require="data.isRequired"></my-foo>

JS:

...
.directive('myFoo', function () {
    return {
    restrict: 'E',
    scope: {
       required: '='
    }
    ...

DEMO

However, this approach does not work as expected because scope.require is undefined. To resolve this issue, the scope definition needs to be modified to

scope: {
    required: '=ngRequired'
}

So, the question arises: what is the best way to handle both situations so that the value is stored in scope.required? Should both definitions be included or should attrs be used from the link function?

Answer №1

There are two main options you can consider:

1. Create a custom form element that supports ng-model

If you look into the source code of the ng-required directive, you will see that it specifically works with the ng-model controller:

restrict: 'A',
require: '?ngModel',
link: function(scope, elm, attr, ctrl) {
  if (!ctrl) return;
  attr.required = true; // ensure truthy value for non-input elements

  ctrl.$validators.required = function(modelValue, viewValue) {
    return !attr.required || !ctrl.$isEmpty(viewValue);
  };

  attr.$observe('required', function() {
    ctrl.$validate();
  });
}

So, if your custom directive already supports ng-model, then it automatically supports ng-required as well. For example:

angular.module('test', [])
.directive('myInput', function(){
  return {
    restrict: 'E',
    require: 'ngModel',
    scope: true,
    template: '<div><button ng-click="changeValue()">Change Value from: {{currentValue}}</button></div>',
    link: function (scope, element, attrs, ngModelCtrl) {
        ngModelCtrl.$parsers.push(function(val){
          if(!val){
            return null;
          }
          return parseFloat(val, 10) * 100;
        });
        ngModelCtrl.$render = function() {
           scope.currentValue = ngModelCtrl.$viewValue || 'No value';
        };
        scope.changeValue = function read(){
          var newValue = Math.random();
          if(newValue > 0.5){
            ngModelCtrl.$setViewValue(newValue + "");
          } else {
            ngModelCtrl.$setViewValue(null);
          }
          ngModelCtrl.$render();
        };
    }
  };
});

2. Modify an existing directive and include ng-required:

angular.module('test', [])
  .directive('myFormElement', function() {
      return {
        restrict: 'E',
        scope: {
          model: '=',
          required: '='
        },
        template: '<div>Enter number: <input type="number" ng-model="data.number" ng-required="required"></div>'
  };

  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<div ng-app="test" ng-init="data={value:'Initial', required: false}">
  <form>
    Is required: <input type="checkbox" ng-model="data.required">
    <my-form-element required="data.required" model="data"></my-form-element>
  </form>
</div>

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

How to send Multipart form data with a string payload

Many suggestions in regards to this issue recommend utilizing some form of FormData within nodejs for building a multipart form. However, I am seeking to achieve the same result without relying on the FormData library. Instead, I aim to use only request h ...

What is the method to determine the overall size of a webpage using the Google PageSpeed API?

"analytics": { "cssResponseBytes": "333 kB", "htmlResponseBytes": "269 kB", "imageResponseBytes": "3.35 MB", "javascriptResponseBytes": "2.29 MB", "numberCssResources": 2, "numberHosts": 80, "numberJsResources": 72, "numberR ...

When trying to use `slug.current` in the link href(`/product/${slug.current}`), it seems to be undefined. However, when I try to log it to the console, it is displaying correctly

import React from 'react'; import Link from 'next/link'; import { urlFor } from '../lib/clients'; const Product = ({ product: { image, name, slug, price } }) => { return ( <div> <Link href={`/product/ ...

Stop the Router from Displaying the Page momentarily prior to Redirecting

Currently, I have set up a session context for my NextJS application where users accessing pages within the /app/ directory are required to undergo an authorization check before being granted access. Although the logic is functioning correctly in redirect ...

Storage in private Safari mode is not synced between tabs on mobile devices

In the private mode of mobile Safari, I have successfully stored data in the localStorage. However, when I open my web app in two separate tabs, I notice that the data stored in one tab is not accessible in the other tab. This behavior may be present in ot ...

How can I eliminate unwanted zombie events in Vue JS?

How do you get rid of a zombie event? When navigating back and forth, the events run multiple times. MainApp.vue <template> <input type="button" @click.prevent="handleClick()" value="Click Me"> </template> <script> export defa ...

Utilizing JSON Data with JQuery: A Beginner's Guide

I am using a setTimeout function to reload another function every 5 seconds. The update_list function is responsible for rendering entrances in a view. However, when there are many entrances and you have scrolled down, the list empties and reloads every e ...

Utilizing a drop-down selection menu and a designated container to store chosen preferences

My form includes a select dropdown that displays available options (populated from a PHP database). Users can choose options from the list, which are then added to a box below to show all selected items. However, I am facing a challenge with the multiple s ...

Preserving the table page number in Angular.js when updating a record in the application

I'm facing an issue with the datatable in my code on a transaction view page. Every time I update a child entry from the datatable and route to the child page, the selected page number is reset to one when I return to the view page. Can someone advise ...

What is the best way to position a popup div in CSS?

Currently, I am in the process of developing a website that displays DVD details when hovering over an image, similar to what is shown in picture 1. However, I've encountered an issue where the content gets cut off for DVDs located on the right side o ...

Issues with jQuery autocomplete functionality on certain elements are not uncommon

I've been experimenting with creating a user script for Opera using Greasemonkey to implement autocomplete functionality on input elements within web pages. However, I've encountered some issues with the script not working as expected. Initially ...

Access various data from the local storage and display them inside a div container

I am trying to display the keys and their values for all the data stored in the local storage. Here is the HTML and JavaScript code I have written: // Setting some values in the local storage localStorage.setItem("lastname", "Smith"); localStorage. ...

Differentiating between ng-show and ng-if in AngularJS

ng-if and ng-show appear to function in a similar manner. <img src="spinner.gif" ng-if="showSpinner"> <img src="spinner.gif" ng-show="showSpinner"> Are there any distinctions between the two? Is there an impact on performance? How can one d ...

Error: Uncaught [🍍]: The function "getActivePinia()" was invoked without an active Pinia instance present. Ensure that you have called "app.use(pinia)" before attempting to utilize a store

I encountered an issue while trying to access a store in Pinia for my Vue web application. Despite installing Pinia and setting app.use(createPinia()), I keep receiving the following error message: Uncaught Error: [ ...

Error occurred: Unable to access 'client' property as it is undefined. Process will continue running

Hi there! I'm currently working on building a key bot for my website, but I keep encountering this error UNCAUGHT EXCEPTION - keeping process alive: TypeError: Cannot read properties of undefined (reading 'client') along with another one rel ...

Having trouble bypassing custom points on the reactive gauge speedometer

My current project involves utilizing the npm package react-d3-speedometer to create a custom points-based gauge. The issue I am facing is that while the package works properly with values from 0 to 1000 when passed to the customSegmentValues property, it ...

Recording a specialized event sent from a web component to React

Trying to incorporate a Lit web component into a React application has presented some challenges for me. This web component is expected to dispatch a custom event at some point, and I need to handle it in the React application appropriately. Despite my li ...

How can I show the initial three digits and last three digits when using ngFor loop in Angular?

Greetings! I have a list of numbers as shown below: array = [1,2,3,4,5,6,7,8,9,10] By using *ngFor, I am displaying the numbers like this: <div *ngFor =" let data of array"> <p>{{data}}</p> </div> Now, instead of d ...

Is the attribute of the label malfunctioning within an Angular directive?

As I delve into the world of angular directives, I have encountered success in many aspects. However, there is one minor issue that has been troubling me lately. Within my directive, I have set a for attribute to match the id of an input field. Strangely, ...

The map fails to load on the HTML page

I am struggling to load a map into my HTML page from a file located in the app folder. Despite using the correct code for inserting the map, it still does not load properly. <!-- Contact section start --> <div id="contact" class="contact"> ...